简体   繁体   中英

I can't seem to resolve error for valueForUndefinedKey:]: whereto class is not key value coding-compliant

The error I can't resolve is ... valueForUndefinedKey:]: this class is not key value coding-compliant for the key managedObjectContext

Here's the code snippet in question (I think) ...

class TripSplitViewController: NSSplitViewController {

@IBOutlet var tripsArrayController: NSArrayController!

var managedObjectContext = (NSApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

I am using Swift 4 and Xcode 9.4 (macOS app) along with Core Data accessing a SQLite data base. I have also created 3 data models with relationships defined between each. I have also create a Split View Controller and am trying to establish my ManagedObjectContext.

One think I have done is check my binds and outlets connections and all look clean and connected correctly.

I am pretty new to Swift and Xcode and this is the first issue I have encountered that all the post on stack responses have not worked in my application.

And one final thing, app was working fine with Core Data until I added the Array Controller to a Split View Controller, the outlet and its connection and the var declaration for the managedObjectContext.

Please, any help is appreciated.

From Using Key-Value Observing in Swift :

Mark properties that you want to observe through key-value observing with both the @objc attribute and the dynamic modifier.

Cocoa Bindings uses KVO. Change

 var managedObjectContext = (NSApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

to

 @objc dynamic var managedObjectContext = (NSApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM