4

It's because My deployment target is less than 10.

how to resolve for deployment target lower to 10.0 ?

enter image description here

4 Answers4

5

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

and add

typealias NSPersistentContainer         = INSPersistentContainer
typealias NSPersistentStoreDescription  = INSPersistentStoreDescription

to your file where you want to use

RolandasR
  • 3,030
  • 2
  • 25
  • 26
3

Not available means not available.

There are two options:

  • Use only the old NSPersistentStoreCoordinator / NSManagedObjectModel pattern.
  • Use both patterns and write the code with availability checking if #available(iOS 10, *)
vadian
  • 274,689
  • 30
  • 353
  • 361
  • These are the only options. There's another discussion here: http://stackoverflow.com/questions/37956720/how-to-create-managedobjectcontext-using-swift-3-in-xcode-8 where you can find ways to use the second approach. – YYamil Dec 21 '16 at 15:50
3

Before iOS 10

you could access the NSManagedObjectContext directly from 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

(Original code)

from iOS 10 and newer

this changed and the NSManagedObjectContext has been moved into the PersistentContainer into the attribute 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].

btw: Be careful with tutorials for versions before iOS 10.

Community
  • 1
  • 1
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
2

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

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
Piotr Tobolski
  • 1,326
  • 7
  • 22
  • What does this mean in terms of running an app on say iOS 8? Can we still run it with iOS 8 this way? Thanks – Munib Jan 05 '17 at 00:51
  • 2
    You won't be able to run this code on iOS 8. Compiler won't let you run this code outside `if #available` if your deployment target is less than availability target for this code. – Piotr Tobolski Jan 09 '17 at 09:55
  • Yeah I found that out the hard way. As much as I don't like it, I had to switch back to the old way of using the core data stack. It works just fine for everything. – Munib Jan 09 '17 at 18:37