gpt4 book ai didi

swift - 如何为 NSPersistentContainer 和 NSPersistentCloudKitContainer 创建同名但根据版本设置的变量?

转载 作者:行者123 更新时间:2023-11-28 13:30:48 25 4
gpt4 key购买 nike

我无法将令人兴奋的核心数据堆栈更改为 NSPersistentCloudKitContainer,因为这只是 iOS 13 及更高版本的支持功能,但我希望为 iOS 13 及更高版本设置该设置,同时为早期版本设置 NSPersistentContainer。

我在使用 swift 和 iOS 方面有相当多的经验,但从来没有为版本控制和某些变量做这样的事情。

这是我在使用 iOS 13 之前的正常操作:

lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "Shopping_App")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

这是我想在 iOS 13 或更高版本的任何设备上使用的代码,但仍然使用 iOS 12 及更早版本的第一位代码。

lazy var persistentContainer: NSPersistentCloudKitContainer = {

let container = NSPersistentCloudKitContainer(name: "Shopping_App")
container.loadPersistentStores(completionHandler: {
(storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

如果可能的话,我希望两个版本有相同的变量而不重新声明错误,如果这可能的话,如果那不可能的话,我想要一个解决方案,它不需要完全重写我所有的存储和检索核心数据、数据。

最佳答案

我找到了答案,结果非常简单。在这里:

lazy var persistentContainer: NSPersistentContainer = {  
if #available(iOS 13.0, *) {
let container = NSPersistentCloudKitContainer(name: "Shopping_App")
container.loadPersistentStores(completionHandler: {
(storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
} else {
let container = NSPersistentContainer(name: "Shopping_App")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {

fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}
}()

关于swift - 如何为 NSPersistentContainer 和 NSPersistentCloudKitContainer 创建同名但根据版本设置的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57365848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com