[英]NSPersistentContainer is only available in 10.0 or newer : error
One of solutions is to use https://github.com/inspace-io/INSPersistentContainer 解决方案之一是使用https://github.com/inspace-io/INSPersistentContainer
and add 并添加
typealias NSPersistentContainer = INSPersistentContainer
typealias NSPersistentStoreDescription = INSPersistentStoreDescription
to your file where you want to use 到你想要使用的文件
Not available means not available. 不可用表示不可用。
There are two options: 有两种选择:
NSPersistentStoreCoordinator / NSManagedObjectModel
pattern. 仅使用旧的 NSPersistentStoreCoordinator / NSManagedObjectModel
模式。 if #available(iOS 10, *)
if #available(iOS 10, *)
请使用这两种模式并使用可用性检查编写代码 Before iOS 10 在iOS 10之前
you could access the NSManagedObjectContext directly from AppDelegate.h
您可以直接从AppDelegate.h
访问NSManagedObjectContext
lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store
// coordinator for the application.) This property is optional since there are legitimate error
// conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
( Original code ) ( 原始代码 )
from iOS 10 and newer 来自iOS 10及更新版本
this changed and the NSManagedObjectContext
has been moved into the PersistentContainer
into the attribute viewContext 这已更改, NSManagedObjectContext
已移入PersistentContainer
进入属性viewContext
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: "")
...
...
( original code ) ( 原始代码 )
So, you need to distinguish which version you're app is running on and then call the correct function. 因此,您需要区分正在运行的应用程序版本,然后调用正确的函数。 ManagedObjectContext inside AppDelegate or The ManagedObjectContext inside [PersistentContainer viewContext]. AppDelegate中的ManagedObjectContext或[PersistentContainer viewContext]中的ManagedObjectContext。
btw: Be careful with tutorials for versions before iOS 10. 顺便说一句:在iOS 10之前的版本教程要小心。
像这样使用@available
标签: @available(iOS 10.0, *) lazy var persistentContainer: NSPersistentContainer = ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.