简体   繁体   中英

Cocoa Bindings to new NSManagedObject

I am new to this, so I will lay out what I am doing and please correct any mistakes.

In my AppDelegate, I have a @property for an NSManagedObject subclass, "Entry". When I press a button on the main window to create a new Entry, I first instantiate the property:

_entry = (Entry*)[NSEntityDescription
                    insertNewObjectForEntityForName:@"Entry"
                    inManagedObjectContext:[self managedObjectContext]];

then I open up a new window by calling

[inputWindow makeKeyAndOrderFront:nil];

This inputWindow has Cocoa Bindings to the entry's properties. When I submit, I do the usual save

[[self managedObjectContext] save:&error];

and then

[inputWindow performClose:nil];

This seems pretty straight forward, but I get an error:

2014-01-11 22:57:32.906 WriteOffs[66441:303] CoreData: error: Failed to call designated initializer on NSManagedObject class 'Entry' 
2014-01-11 22:57:32.943 WriteOffs[66441:303] An uncaught exception was raised
2014-01-11 22:57:32.943 WriteOffs[66441:303] [<Entry 0x6100000ba040> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "date".

It seems that entry is null, implying that the inputWindow and its bindings are instantiated before I click the button on the main window to create the new entry and open up the inputWindow.

Seems like there is a simple solution that I am missing.

EDIT:

Here's what I have. In WOAppDelegate.h,

#import <Cocoa/Cocoa.h>
#import "Entry.h"

@interface WOAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSWindow *inputWindow;

@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

- (IBAction)saveAction:(id)sender;

@property (weak, nonatomic) Entry *entry;


@end

Then in WOAppDelegate.m

#import "WOAppDelegate.h"

@implementation WOAppDelegate

@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize managedObjectContext = _managedObjectContext;

@synthesize inputWindow = _inputWindow;
@synthesize entry = _entry;

// Core Data Stuff...

Entry.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface Entry : NSManagedObject

@property (nonatomic, retain) NSNumber * amount;
@property (nonatomic, retain) NSString * category;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSString * payee;
@property (nonatomic, retain) NSString * text;

@end

Entry.m

#import "Entry.h"

@implementation Entry

@dynamic amount;
@dynamic category;
@dynamic date;
@dynamic images;
@dynamic payee;
@dynamic text;

@end

Pretty simple. In my MainMenu.xib, I have the main window, and the inputWindow. I have an array controller binded to the managedObjectContext that fills in content for the table on the main window. I also have a second window for entering in new entries to the table. This is bonded to the property, entry.

Thanks again for the help,

Chet

Does your “Entry” class maybe override some of the methods in NSManagedObject? Maybe even

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context;    

If so, do you fail to call -super in any of your subclass's methods?

In the array controller, I had mode:Class referencing the Entry class. I changed that to mode:Entity referencing the Entry entity and everything worked out. :)

Can someone comment on why the debugger is so bad at giving a meaningful response? Where in the error message could have pointed me to this being the error?

2014-01-12 18:35:31.520 WriteOffs[68395:303] CoreData: error: Failed to call designated initializer on NSManagedObject class 'Entry' 
2014-01-12 18:35:31.555 WriteOffs[68395:303] An uncaught exception was raised
2014-01-12 18:35:31.555 WriteOffs[68395:303] [<Entry 0x6100000a9a20> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "date".
2014-01-12 18:35:31.556 WriteOffs[68395:303] (
    0   CoreFoundation                      0x00007fff8b93f41c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff944f3e75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8b93efc9 -[NSException raise] + 9
    3   CoreData                            0x00007fff89650b14 -[NSManagedObject valueForUndefinedKey:] + 276
    4   Foundation                          0x00007fff91eff49f -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 334
    5   AppKit                              0x00007fff89c872d3 -[NSBinder valueForBinding:atIndex:resolveMarkersToPlaceholders:] + 408
    6   AppKit                              0x00007fff89963567 -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 622
    7   AppKit                              0x00007fff8a022008 -[NSValueBinder updateTableColumnDataCell:forDisplayAtIndex:] + 143
    8   AppKit                              0x00007fff89f9e2ee -[NSTextValueBinder updateTableColumnDataCell:forDisplayAtIndex:] + 73
    9   CoreFoundation                      0x00007fff8b873639 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 137
    10  CoreFoundation                      0x00007fff8b872ddf -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 319
    11  AppKit                              0x00007fff8a062ac7 -[_NSBindingAdaptor tableColumn:willDisplayCell:row:] + 108
    12  AppKit                              0x00007fff89b8ada0 -[NSTableView preparedCellAtColumn:row:] + 615
    13  AppKit                              0x00007fff89b8a9ee -[NSTableView _drawContentsAtRow:column:withCellFrame:] + 44
    14  AppKit                              0x00007fff89b8a723 -[NSTableView drawRow:clipRect:] + 1629
    15  AppKit                              0x00007fff89b89f7d -[NSTableView drawRowIndexes:clipRect:] + 776
    16  AppKit                              0x00007fff89a534c2 -[NSTableView drawRect:] + 1484
    17  AppKit                              0x00007fff89a2c399 -[NSView _drawRect:clip:] + 3748
    18  AppKit                              0x00007fff89a2ac0e -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1799
    19  AppKit                              0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
    20  AppKit                              0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
    21  AppKit                              0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
    22  AppKit                              0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
    23  AppKit                              0x00007fff89a28a50 -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 841
    24  AppKit                              0x00007fff89a28201 -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 314
    25  AppKit                              0x00007fff89a25209 -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 2828
    26  AppKit                              0x00007fff89a0463a -[NSView displayIfNeeded] + 1680
    27  AppKit                              0x00007fff89a1e246 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1972
    28  AppKit                              0x00007fff89a1d7f0 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 786
    29  AppKit                              0x00007fff89a1d470 -[NSWindow orderWindow:relativeTo:] + 162
    30  AppKit                              0x00007fff898bbc26 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1406
    31  AppKit                              0x00007fff898b05fe loadNib + 327
    32  AppKit                              0x00007fff898afb9c +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 288
    33  AppKit                              0x00007fff898af98c -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 197
    34  AppKit                              0x00007fff898af77b +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 357
    35  AppKit                              0x00007fff898ab617 NSApplicationMain + 448
    36  WriteOffs                           0x0000000100002e02 main + 34
    37  libdyld.dylib                       0x00007fff8eddf5fd start + 1
    38  ???                                 0x0000000000000003 0x0 + 3
)
2014-01-12 18:35:31.640 WriteOffs[68395:303] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Entry 0x6100000a9a20> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "date".'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff8b93f41c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff944f3e75 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8b93efc9 -[NSException raise] + 9
    3   CoreData                            0x00007fff89650b14 -[NSManagedObject valueForUndefinedKey:] + 276
    4   Foundation                          0x00007fff91eff49f -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 334
    5   AppKit                              0x00007fff89c872d3 -[NSBinder valueForBinding:atIndex:resolveMarkersToPlaceholders:] + 408
    6   AppKit                              0x00007fff89963567 -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 622
    7   AppKit                              0x00007fff8a022008 -[NSValueBinder updateTableColumnDataCell:forDisplayAtIndex:] + 143
    8   AppKit                              0x00007fff89f9e2ee -[NSTextValueBinder updateTableColumnDataCell:forDisplayAtIndex:] + 73
    9   CoreFoundation                      0x00007fff8b873639 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 137
    10  CoreFoundation                      0x00007fff8b872ddf -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 319
    11  AppKit                              0x00007fff8a062ac7 -[_NSBindingAdaptor tableColumn:willDisplayCell:row:] + 108
    12  AppKit                              0x00007fff89b8ada0 -[NSTableView preparedCellAtColumn:row:] + 615
    13  AppKit                              0x00007fff89b8a9ee -[NSTableView _drawContentsAtRow:column:withCellFrame:] + 44
    14  AppKit                              0x00007fff89b8a723 -[NSTableView drawRow:clipRect:] + 1629
    15  AppKit                              0x00007fff89b89f7d -[NSTableView drawRowIndexes:clipRect:] + 776
    16  AppKit                              0x00007fff89a534c2 -[NSTableView drawRect:] + 1484
    17  AppKit                              0x00007fff89a2c399 -[NSView _drawRect:clip:] + 3748
    18  AppKit                              0x00007fff89a2ac0e -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1799
    19  AppKit                              0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
    20  AppKit                              0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
    21  AppKit                              0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
    22  AppKit                              0x00007fff89a2afea -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 2787
    23  AppKit                              0x00007fff89a28a50 -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 841
    24  AppKit                              0x00007fff89a28201 -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 314
    25  AppKit                              0x00007fff89a25209 -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 2828
    26  AppKit                              0x00007fff89a0463a -[NSView displayIfNeeded] + 1680
    27  AppKit                              0x00007fff89a1e246 -[NSWindow _reallyDoOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 1972
    28  AppKit                              0x00007fff89a1d7f0 -[NSWindow _doOrderWindow:relativeTo:findKey:forCounter:force:isModal:] + 786
    29  AppKit                              0x00007fff89a1d470 -[NSWindow orderWindow:relativeTo:] + 162
    30  AppKit                              0x00007fff898bbc26 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1406
    31  AppKit                              0x00007fff898b05fe loadNib + 327
    32  AppKit                              0x00007fff898afb9c +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 288
    33  AppKit                              0x00007fff898af98c -[NSBundle(NSNibLoading) loadNibNamed:owner:topLevelObjects:] + 197
    34  AppKit                              0x00007fff898af77b +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 357
    35  AppKit                              0x00007fff898ab617 NSApplicationMain + 448
    36  WriteOffs                           0x0000000100002e02 main + 34
    37  libdyld.dylib                       0x00007fff8eddf5fd start + 1
    38  ???                                 0x0000000000000003 0x0 + 3
)
libc++abi.dylib: terminating with uncaught exception of type NSException

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