Core data Swift 3中的managedObjectContext

Core data Swift 3中的managedObjectContext,core-data,swift3,macos-sierra,Core Data,Swift3,Macos Sierra,我想研究使用Swift和CoreData创建表的方法。然而,使用Swift 3我无法让它工作。最重要的是,我不能正确地更换线路 // set up the NSManagedObjectContext let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate managedContext = appDelegate.managedObjectContext 即使我发现了(不过这是iOS而不是

我想研究使用Swift和CoreData创建表的方法。然而,使用Swift 3我无法让它工作。最重要的是,我不能正确地更换线路

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

即使我发现了(不过这是iOS而不是OSX)。我如何替换生成错误消息的代码段“AppDelegate”类型的值没有成员“managedContext”?

macOS中的Swift 3

let appDelegate = NSApplication.shared().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
您提供的错误表示
'AppDelegate'没有成员“managedContext”
,而
'AppDelegate'没有成员“managedObjectContext”
,这将导致我假设您只需要修复语法

iOS 10中的Swift 3

核心数据至少需要3样东西才能工作:

  • 托管对象模型
  • 持久存储协调器
  • 和托管对象上下文
  • 把这三件事放在一起,就得到了核心数据堆栈

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

    回答了如何创建容器对象

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

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

    这是一篇很有帮助的文章,但如果这篇文章读起来有点太重,那么这篇文章就可以很好地解释这个主题。

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

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

    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
    

    对于macOS和Swift 3.1,这将非常有效

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

    创建新项目时,您是否选中了“使用核心数据”选项?它是必需的,因为它为AppDelegate中的核心数据堆栈添加了代码。@vadian是的,我添加了。但是:我也检查了基于文档的应用程序、单元测试和UI测试。我注意到,当我检查所有内容时,AppDelegate中没有代码,而只检查CoreData…这很奇怪。提交一个bug。要解决您的问题,请创建一个仅检查核心数据的新项目,并将核心数据堆栈复制并粘贴到基于文档的项目中。适用于Swift4