NSPersistentContainer 仅在 10.0 或更新版本中可用:错误
NSPersistentContainer is only available in 10.0 or newer : error
因为我的部署目标小于10
如何解决部署目标低于 10.0 的问题?
像这样使用 @available
标签:
@available(iOS 10.0, *)
lazy var persistentContainer: NSPersistentContainer = ...
不可用表示不可用。
有两种选择:
- 仅使用 旧
NSPersistentStoreCoordinator / NSManagedObjectModel
模式。
- 同时使用这两种模式并编写带有可用性检查的代码
if #available(iOS 10, *)
解决方案之一是使用 https://github.com/inspace-io/INSPersistentContainer
并添加
typealias NSPersistentContainer = INSPersistentContainer
typealias NSPersistentStoreDescription = INSPersistentStoreDescription
到您要使用的文件
之前 iOS 10
您可以直接从 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
来自 iOS 10 和更新
这已更改,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)
因此,您需要区分您的应用 运行 是哪个版本,然后调用正确的函数。 AppDelegate 内的 ManagedObjectContext 或 [PersistentContainer viewContext] 内的 ManagedObjectContext。
顺便说一句:请注意 iOS 10 之前版本的教程。
因为我的部署目标小于10
如何解决部署目标低于 10.0 的问题?
像这样使用 @available
标签:
@available(iOS 10.0, *)
lazy var persistentContainer: NSPersistentContainer = ...
不可用表示不可用。
有两种选择:
- 仅使用 旧
NSPersistentStoreCoordinator / NSManagedObjectModel
模式。 - 同时使用这两种模式并编写带有可用性检查的代码
if #available(iOS 10, *)
解决方案之一是使用 https://github.com/inspace-io/INSPersistentContainer
并添加
typealias NSPersistentContainer = INSPersistentContainer
typealias NSPersistentStoreDescription = INSPersistentStoreDescription
到您要使用的文件
之前 iOS 10
您可以直接从 AppDelegate.h
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
来自 iOS 10 和更新
这已更改,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)
因此,您需要区分您的应用 运行 是哪个版本,然后调用正确的函数。 AppDelegate 内的 ManagedObjectContext 或 [PersistentContainer viewContext] 内的 ManagedObjectContext。
顺便说一句:请注意 iOS 10 之前版本的教程。