1

I'm insert in my Xcode project (Swift 2.2 and Xcode 7.2.1) a code into Appdelegate file to CoreData.

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: "ArduinoHomeKit")
        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
    }()
    
    func saveContext () {
        let context  = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // abort() 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.
                let nserror = error as NSError
                NSLog("Unresolved error \(nserror), \(nserror.userInfo)")
                abort()
            }
        }
    }

But I have an error on NSPersistentContainer. The error is:

could not infer type for 'persistentContainer'

use of undeclared type 'NSPersistenContainer'

I think that this problem is because the code is for Swift 3.0 but I have used Swift 2.2 How can I fixed it?

Community
  • 1
  • 1
bircastri
  • 2,169
  • 13
  • 50
  • 119

2 Answers2

3

NSPersistentContainer was added in iOS 10. You are using Xcode 7.2.1 which means your Base SDK is iOS 9. Therefore NSPersistentContainer is an unknown class.

Use older examples for Core Data that existed prior to iOS 10 and NSPersistentContainer.

Or update to Xcode 8.3 and iOS 10 (which you really should do anyway). But if your Deployment Target is iOS 9 or earlier, you need to be aware that your app won't work on those device if you try to use NSPersistentContainer.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Ok, but what class I can use to iOS 9? – bircastri Apr 18 '17 at 11:59
  • There are many Core Data examples out there that haven't been updated for iOS 10. Do some searching. You can also take a look at http://stackoverflow.com/a/39627078/1226963 for an example of supporting both the old and new way. – rmaddy Apr 18 '17 at 15:04
0

Well, the type NSPersistenContainer is indeed not declared anywhere. The type NSPersistentContainer is. So wherever you are trying to use NSPersistenContainer, use NSPersistentContainer instead.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 1
    None of posted code has that typo, just the 2nd error message. Probably a typo when posting the question. – rmaddy Apr 14 '17 at 15:37