2016-08-29 7 views
10

this example codeには、SwiftとCoreDataを使用してテーブルを作成します。しかし、Swift 3を使用すると、それを動作させることができません。最も重要なのは、私はきちんと私が(ただしiOSのは、OS Xのではありません)this related questionを発見したにも関わらずラインにSwift 3のmanagedObjectContext

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

を置き換えることはできません。エラーメッセージValue of type 'AppDelegate' has no member 'managedContext'を生成するコードをどのように置き換えることができますか?

+0

新しいプロジェクトを作成したときに[コアデータを使用する]オプションをチェックしましたか?これは、AppDelegateにコアデータスタック用のコードを追加するために必要です。 – vadian

+0

@vadianはいそうでした。しかし、私はドキュメントベースのアプリ、ユニットテスト、UIテストもチェックしました。 CoreDataをチェックするのとは対照的に、すべてをチェックするとAppDelegateにコードがないことに気付きました。 – DaPhil

+0

それは変です。バグを報告してください。問題を解決するには、コアデータのみがチェックされた新規プロジェクトを作成し、コアデータスタックを文書ベースのプロジェクトにコピー&ペーストします。 – vadian

答えて

13

MacOSのでスウィフト3

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

は、あなたが提供されるエラーは、あなたは自分の構文を修正する必要があると仮定するために私につながる'AppDelegate' has no member 'managedContext'代わりの'AppDelegate' has no member 'managedObjectContext'を言います。

のiOSで

スウィフト3 10

Core Dataは動作するように、少なくとも3つの事を必要とします:

  1. 管理対象オブジェクトモデル
  2. 永続ストアコーディネータ
  3. そして、管理対象オブジェクトコンテキスト

これら3つのものコアデータスタックを取得します。

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このコードで設定を取得することができます唯一の

// 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 

を使用

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

AppDelegateは、メンバーの下に持っている。これは、正常に動作します

2

macOSとSwift 3.1の場合

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