私はCoreDataとmacOSに取り組んでから、しばらくしてきました.xibとnibの時代に戻ってきました。 xibには、ドキュメントとmanagedObjectContextへのアクセスを提供する「File's Owner」があります。簡単です。NSPersistentDocument、Swift、macOS、storyboards - managedObjectContextの取得方法は?
NSPersistentDocumentと私のストーリーボードでは、ちょっとした鶏と卵の問題があります。
override func makeWindowControllers() {
// Returns the Storyboard that contains your Document window.
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateControllerWithIdentifier("Document Window Controller") as! NSWindowController // <- when I need the moc
self.addWindowController(windowController)
windowController.contentViewController!.representedObject = self // <- when I set the representedObject
}
これは、Appleを含む多くの人々が、示唆されているもののようだ:NSPersistentDocumentからサブクラス私のDocumentクラスでは、私は以下のものを持っています。
MainViewControllerではObjectControllerが必要ですが、managedObjectContextにバインドする必要がありますが、managedObjectContextを設定する必要がある場合は、selfにselfをまだ設定していません。したがって、例外がスローされます。 makeWindowControllersメソッドの最後にrepresentObjectを設定するのは遅すぎますが、それ以前の方法では表示されません。