0

代码

    let managedObjectContext =
        (UIApplication.sharedApplication.delegate
            as! AppDelegate).managedObjectContext
    

错误:

  • 类型的值AppDelegate没有成员managedObjectContext

我的问题是我想managedObjectContext在 Xcode 8 中使用,但它说AppDelegate没有这样的成员。我想使用它,因为我想用核心数据为 ios 9 创建一个项目。我想要定义managedObjectContext,如果你有请评论

4

2 回答 2

-1

解释:

  • 错误很清楚,它指出AppDelegate没有名为managedObjectContext. 检查AppDelegate并查看是否存在具有该名称的属性managedObjectContext

参考:

在编写代码之前学习以下内容非常重要:

于 2017-05-17T13:10:28.527 回答
-1

您只需创建一个带有“使用核心数据”标记的项目,它将在您项目的应用程序委托中可用。项目创建和选择使用核心数据标记的图像

    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: "temp")
    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
}()

在应用程序委托中使用

let context = persistentContainer.viewContext
于 2017-05-17T13:43:17.053 回答