Swift 3中的managedObjectContext

DaP*_*hil 12 core-data swift3 macos-sierra

我想通过这个示例代码来处理,其中使用Swift和CoreData来创建表.但是,使用Swift 3我无法使用它.最重要的是,我无法正确更换线路

// set up the NSManagedObjectContext
  let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
  managedContext = appDelegate.managedObjectContext
Run Code Online (Sandbox Code Playgroud)

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

dar*_*nge 17

MacOS中的Swift 3

let appDelegate = NSApplication.shared().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
Run Code Online (Sandbox Code Playgroud)

您提供的错误'AppDelegate' has no member 'managedContext'代替'AppDelegate' has no member 'managedObjectContext',而不是,这将导致我假设您只需要修复您的语法.

iOS 10中的Swift 3

核心数据至少需要3件工作:

  1. 托管对象模型
  2. 持久性商店协调员
  3. 和托管对象上下文

把这三件事放在一起,就可以得到核心数据堆栈.

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

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

managedObjectContext现在是一个名为的财产viewContext,通过以下方式访

let delegate = UIApplication.shared.delegate as! AppDelegate
let managedObjectContext = delegate.persistentContainer.viewContext
Run Code Online (Sandbox Code Playgroud)

一篇有用的文章是核心数据中的新功能,但如果这个阅读看起来有点过重,那么这个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
}()
Run Code Online (Sandbox Code Playgroud)

所以用

 let managedContext = (UIApplication.shared.delegate as! appDelegate).persistentContainer.viewContext
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作


Ped*_*Luz 5

对于macOS和Swift 3.1

let moc: NSManagedObjectContext = (NSApplication.shared().delegate as! AppDelegate).persistentContainer.viewContext
Run Code Online (Sandbox Code Playgroud)