NSPersistentContainer is only available in version 10.0 or later: error

This is because my deployment goal is less than 10.

How to solve the deployment problem before 10.0?

enter image description here

+4
source share
4 answers

One solution is to use https://github.com/inspace-io/INSPersistentContainer

and add

typealias NSPersistentContainer         = INSPersistentContainer
typealias NSPersistentStoreDescription  = INSPersistentStoreDescription

to the file you want to use

+4
source

Not available means not available.

There are two options:

  • Use only the old template NSPersistentStoreCoordinator / NSManagedObjectModel.
  • Use both templates and write accessibility code if #available(iOS 10, *)
+2
source

IOS 10

NSManagedObjectContext 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: "")
...
...

( )

, , , . ManagedObjectContext AppDelegate ManagedObjectContext [PersistentContainer viewContext].

btw: iOS 10.

+2
source

Use the tag @availableas follows: @available(iOS 10.0, *) lazy var persistentContainer: NSPersistentContainer = ...

+1
source

Source: https://habr.com/ru/post/1656688/


All Articles