2016-08-29 142 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