2016-08-29 101 views
10

我想通过this example code工作,其中Swift和CoreData用于创建表。但是,使用Swift 3我没有得到它的工作。最重要的是,我不能正确地更换线swift 3中的managedObjectContext

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

即使我发现this related question(然而这是iOS的不是OS X)。我该如何更换产生错误信息Value of type 'AppDelegate' has no member 'managedContext'的那段代码?

+0

您是否在创建新项目时选中了“使用核心数据”选项?这是必需的,因为它在AppDelegate中添加了Core Data Stack的代码。 – vadian

+0

@vadian是的,我做到了。但是:我还检查了基于文档的应用程序,单元测试和UI测试。我注意到,当我检查所有内容时,AppDelegate中没有代码,而只检查CoreData ... – DaPhil

+0

这很奇怪。提交一个错误。为了解决您的问题,只检查Core Data创建一个新项目,并将Core Data Stack复制并粘贴到基于文档的项目中。 – vadian

回答

13

斯威夫特3 MACOS

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

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

斯威夫特3 iOS的10

核心数据至少需要三样东西的工作:

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

把这三样东西s一起,你会得到Core Data Stack。

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

如何创建容器对象的回答是here

managedObjectContext现在是一个叫viewContext属性,通过访问:

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

的实用文章为What's New in Core Data,但如果读数似乎有点过重,这WWDC video解释了这个话题了伟大的工作。

2

我迅速3,你可以通过这个代码得到managedContext设置:

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

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 

这将正常工作

2

对于macOS和Swift 3.1

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