12

我想完成这个使用 Swift 和 CoreData 创建表的示例代码。但是,使用 Swift 3 我无法让它工作。最重要的是,我无法正确更换线路

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

即使我发现了这个相关的问题(但它是 iOS 而不是 OS X)。如何替换产生错误消息的那段代码Value of type 'AppDelegate' has no member 'managedContext'

4

4 回答 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. 和一个托管对象上下文

把这三样东西放在一起,你就得到了核心数据栈。

当 iOS 10 出现时,引入了一个名为NSPersistentContainer的新对象,它封装了核心数据堆栈。

这里回答了如何创建容器对象。

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

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

一篇很有帮助的文章是What's New in Core Data,但如果阅读内容看起来有点过于繁重,这个WWDC 视频很好地解释了这个主题。

于 2016-08-31T00:06:40.680 回答
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
}()

所以用

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

这将正常工作

于 2016-10-05T07:33:01.693 回答
5

适用于 macOS 和 Swift 3.1

let moc: NSManagedObjectContext = (NSApplication.shared().delegate as! AppDelegate).persistentContainer.viewContext
于 2017-07-08T11:42:25.993 回答
4

I swift 3 您可以通过以下代码获取 managedContext 设置:

  let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
于 2016-09-28T09:41:54.833 回答