gpt4 book ai didi

ios - Swift 3-“NSPersistentContainer仅在ios 10和更高版本上可用”

转载 作者:行者123 更新时间:2023-12-01 17:28:52 24 4
gpt4 key购买 nike

因此,当我尝试将部署目标设置为iOS 9时(确实低于10.0),我得到了标题中所述的错误。

这里存在问题:

// MARK: - Core Data stack

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: "Keebin_development_1")
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
}()

查看不同的SO问题/答案,很明显,我需要添加一些代码来区分iOS 10是否可用。 Swift 3本身建议使用 @available(iOS 10.0, *)。但这还不够。我期望这是因为我错过了“如果不可用,请使用此功能”,但是对于Swift和iOS编程来说我是新手,所以我不确定该写些什么。而且我似乎找不到任何答案,无法给出关于所写内容的确切答案。有人可以帮忙吗?

最佳答案

您确实不需要将iOS 10与早期版本区分开。 NSPersistentContainer需要iOS 10,但是较旧的技术仍然有效并且仍受支持。如果您需要支持iOS <10,请不要使用NSPersistentContainer。加载托管对象模型和添加持久性存储要采取更多步骤,但这意味着您可以为所有版本的iOS使用单个代码路径。

关于ios - Swift 3-“NSPersistentContainer仅在ios 10和更高版本上可用”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132799/

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