How do I bind my Array Controller to my core data model?

假装没事ソ 提交于 2019-11-29 12:37:57

In a storyboard based project there is no (binding) reference from a view controller to the AppDelegate class.

A solution is to add a property and override init?(coder in the view controller

@objc let managedObjectContext: NSManagedObjectContext

required init?(coder: NSCoder) {
    self.managedObjectContext = (NSApp.delegate as! AppDelegate).persistentContainer.viewContext
    super.init(coder: coder)
}

then bind the ManagedObjectContext to ViewController -> managedObjectContext.

In the Attribute Inspector of the array controller don't forget to set the Mode to Entity Name, insert the entity name and check Prepares Content.

To fix [<Core_Data_Binding.ViewController 0x6080000c4600> valueForUndefinedKey:]: this class is not key value coding-compliant for the key managedObjectContext change let managedObjectContext: NSManagedObjectContext in vadian's code to @objc let managedObjectContext: NSManagedObjectContext. XCode 9 by default does not expose properties and methods to Objective C: The use of Swift 3 @objc inference in Swift 4 mode is deprecated?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!