开发者

this class is not key value coding-compliant for the key managedObjectContext

Update! I've created a sanitized test program that illustrates the issue.

Here you can view a PDF of exactly what I've done, and download a .zip of the project

Update2! See the comment thread of Conceited Code below to understand more about this problem.

Update: Added bounty, I'm looking for an explanation of what's actually wrong with my example, what fixes it, and what design better suits my purpose than the current "LoadNib_Controller" (see Conceited Code comment discussion)

I get a runtime error message " [<My_WindowLoader 0x100228ba0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key managedObjectContext. "

My_WindowLoader has a reference to My_AppDelegate (which I've debugged and is definitely set up properly). It also has a few My_WindowController *'s.. which are My_WindowController : NSWindowController. They are used so that I can provide a property (managedObjectContext) to Windows that load up as I load nib's up, so that I can set up my Core Data bindings. They look like..

.h:
@interface My_WindowController : NSWindowController {

NSManagedObjectContext *managedObjectContext;

}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

.m
@synthesize managedObjectContext;

My_WindowLoader loads a nib in the following manner:

Window1_WindowController = [[My_ WindowController alloc] initWithWindowNibName:@"Window1" owner:self];
// The following line I have debugged and it does in fact go to my app delegate, wherein it instantiates a MOC (since this is the first place in the code that it's used..), and returns it, and it appears to also be setting it via the property in My_WindowController
[Window1_WindowController setManagedObjectContext:[AppDelegate managedObjectContext]];

// The following is where I get my problem, this does load my window but in my gdb window I see the runtime error message that I list at the top of this post 
NSWindow *window1Pointer 开发者_运维技巧= [Window1_WindowController window];

To get the above runtime error, within my Window1.nib I have array controllers that bind their Managed Object Context to "File Owner" with model key path "managedObjectContext". File's Owner is type My_WindowController

Obviously something is wrong but I've stepped through it with a debugger as well as using common sense, and it all seems like it should work. I see most "this class is not kvc.." errors stem from misnaming stuff in IBOutlet/IB etc but I have triple checked all of that here and I have ensured that I completely quit and restarted Xcode/IB and have the latest loaded.


Thanks to #cocoa on freenode, my issue is that I'm doing:

Window1_WindowController = [[My_ WindowController alloc] initWithWindowNibName:@"Window1" owner:self];

I should be doing

Window1_WindowController = [[My_ WindowController alloc] initWithWindowNibName:@"Window1"];

Note the difference with initWithWindowNibName: vs initWithWindowNibName:owner:

Furthermore, the right place to put this code is in AppDelegate, it's the App controller and this type of logic is at home there.


The problem is being caused by the NSArrayController in the new window that you have binded to your managedObjectContext. If you uncheck "raises for inapplicable keys" the error becomes "Cannot perform operation without a managed object context". I believe there is something wrong with the managed object context in your class.

EDIT: see comments


Does your NIB have an NSWindow correctly connected up to File's Owner (Window1_WindowController)?


Your file's owner is the wrong object. It's supposed to be the window controller, the one with the managedObjectContext property. Why did you make a separate controller to load the nib? That's not standard practise. NSWindowController is designed to load its own nib. It should be the file's owner. It should be instantiated with [[MyWindowController alloc] initWithNibName:@"MyWindow"].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜