This class is not a key value compatible with the encoding for the managedObjectContext key

Update! I created a sanitized test program that illustrates the problem.
Here you can view the PDF file of exactly what I did and download the project .zip

Update2! See explanations for the comments below to learn more about this issue.

Update: a bounty has been added, I'm looking for an explanation of what is actually wrong with my example, what fixes it, and which design is better for my purpose than the current "LoadNib_Controller" (see the discussion of comments with comments)

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

My_WindowLoader has a link to My_AppDelegate (which I debugged and definitely configured correctly). It also has several My_WindowController *, which are My_WindowController: NSWindowController. They are used so that I can provide the (managedObjectContext) property for Windows, loaded when nib boots, so I can configure Core Data bindings. They look like ..

.h:
@interface My_WindowController : NSWindowController {

NSManagedObjectContext *managedObjectContext;

}

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

.m
@synthesize managedObjectContext;

My_WindowLoader loads the tip as follows:

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 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];

, Window1.nib , " " "managedObjectContext". - My_WindowController

, - , , , , . , " kvc.." IBOutlet/IB .., , , Xcode/IB .

+3
4

# cocoa freenode , :

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

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

initWithWindowNibName: vs initWithWindowNibName: owner:

, AppDelegate, .

+3

NSArrayController , . " ", " ". , - .

EDIT: .

+1

NIB NSWindow, File Owner (Window1_WindowController)?

0

Your file owner is the wrong object. It must be a window controller that has the managedObjectContext property. Why did you create a separate controller for loading the tip? This is not standard practice. NSWindowController is designed to load its own tip. He must be the owner of the file. It must be created using [[MyWindowController alloc] initWithNibName: @ "MyWindow"].

0
source

Source: https://habr.com/ru/post/1755257/


All Articles