Ios 未声明的类型';NSpersistenceContainer';

Ios 未声明的类型';NSpersistenceContainer';,ios,swift,xcode,core-data,Ios,Swift,Xcode,Core Data,我正在我的Xcode项目(Swift 2.2和Xcode 7.2.1)中插入一段代码到Appdelegate文件中,以保存CoreData lazy var persistentContainer: NSPersistentContainer = { /* The persistent container for the application. This implementation creates and returns a contain

我正在我的Xcode项目(Swift 2.2和Xcode 7.2.1)中插入一段代码到Appdelegate文件中,以保存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()
            }
        }
    }
但是我在NSPersistentContainer上有一个错误。 错误是:

无法推断“persistentContainer”的类型

使用未声明的类型“NSPersistenceContainer”

我认为这个问题是因为代码是针对Swift 3.0的,但我使用了Swift 2.2
如何修复它?

iOS 10中添加了NSPersistentContainer。您使用的是Xcode 7.2.1,这意味着您的基本SDK是iOS 9。因此,
NSPersistentContainer
是一个未知类

对于iOS 10和
NSPersistentContainer
之前存在的核心数据,请使用较旧的示例


或者更新到Xcode 8.3和iOS 10(无论如何,您确实应该这样做)。但是,如果您的部署目标是iOS 9或更早版本,则需要注意,如果您尝试使用
NSPersistentContainer

,则您的应用程序将无法在这些设备上运行。嗯,NSPersistentContainer类型确实没有在任何地方声明。容器的类型为。因此,无论您在何处尝试使用NSPersistentContainer,请改用NSPersistentContainer

更新Xcode/您的代码。Swift 3和更高版本是未来。要在Swift 3.0上更新,我必须更新Xcode?是的,你必须更新。好的,我正在尝试更新3小时前的Xcode,但我有一个在Apple Store上运行的圆圈,但安装没有开始。太慢了发布的代码中有一个输入错误,只是第二条错误消息。在发布问题时可能是输入错误。好吧,但是我可以使用什么类来发布iOS 9?有许多核心数据示例尚未针对iOS 10进行更新。做一些搜索。您还可以查看支持新旧方式的示例。