xib와 nibs의 시대에 CoreData와 macOS를 다루기 시작한 지 꽤 오래되었습니다. xib에는 문서와 managedObjectContext에 대한 액세스 권한을 부여 할 수있는 "파일 소유자"가 있습니다. 쉬운.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
}
이 애플을 포함한 많은 사람들이, 제안하는 어떤 것 같습니다 : NSPersistentDocument에서 서브 클래스 내 문서 클래스에서, 나는 다음 있습니다.
내 문제는 다음과 같습니다. MainViewController에서 ObjectController가 필요하며 managedObjectContext에 바인딩해야하지만 managedObjectContext가 필요할 때 self로 아직 representObject를 설정하지 않았습니다. 따라서 예외가 발생합니다. makeWindowControllers 메서드의 끝에서 representObject를 설정하는 것은 너무 늦었지만 어쨌든 이전에 가져올 수는 없습니다.