在Swift 3中的managedObjectContext

12

我想要使用Swift和CoreData创建一个表格,因此希望通过这个示例代码进行学习。但是,使用Swift 3时,我无法让它正常工作。最重要的是,我无法正确地替换该行:

// set up the NSManagedObjectContext
  let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
  managedContext = appDelegate.managedObjectContext

虽然我找到了这个相关问题 (但是这是关于iOS而不是OS X)。 我该如何替换掉那段代码,以避免出现错误消息“AppDelegate类型的值没有'managedContext'成员”?


你在创建新项目时是否勾选了“使用Core Data”选项?这是必需的,因为它会在AppDelegate中添加Core Data Stack的代码。 - vadian
@vadian 是的,我做了。但是:我还检查了基于文档的应用程序、单元测试和UI测试。当我检查所有内容与仅检查CoreData相比时,我注意到AppDelegate中没有任何代码... - DaPhil
很奇怪。报告 bug。为解决您的问题,请创建一个仅选择 Core Data 的新项目,并将 Core Data Stack 复制并粘贴到您的基于文档的项目中。 - vadian
4个回答

18

macOS中的Swift 3

let appDelegate = NSApplication.shared().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext

您提供的错误信息为'AppDelegate' has no member 'managedContext'而不是'AppDelegate' has no member 'managedObjectContext',这让我觉得您只需要修正语法问题。

iOS 10中的Swift 3

Core Data需要至少三个东西才能工作:

  1. 管理对象模型
  2. 持久化存储协调器
  3. 和管理对象上下文

将这三个东西放在一起,就得到了Core Data堆栈。

iOS 10推出了一个称为NSPersistentContainer的新对象,它封装了核心数据堆栈。

如何创建容器对象在此处有所回答。

managedObjectContext现在是一个名为viewContext的属性,可以通过以下方式访问:

let delegate = UIApplication.shared.delegate as! AppDelegate
let managedObjectContext = delegate.persistentContainer.viewContext

一篇有帮助的文章是《Core Data有什么新变化》,但如果那篇阅读起来感觉有些沉重,这个WWDC视频做得很好地解释了这个主题。


8

AppDelegate 只有以下成员:

// MARK: - Core Data stack

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

所以使用 <\p>
 let managedContext = (UIApplication.shared.delegate as! appDelegate).persistentContainer.viewContext

这将正常工作


5

适用于 macOS 和 Swift 3.1

let moc: NSManagedObjectContext = (NSApplication.shared().delegate as! AppDelegate).persistentContainer.viewContext

4

在Swift 3中,您可以通过以下代码获取managedContext:

  let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

适用于Swift4。 - J A S K I E R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接