Objective c 新NSManagedObject的Cocoa绑定
我是新来的,所以我会说明我在做什么,请纠正任何错误 在我的AppDelegate中,NSManagedObject子类Entry有一个@property。当我按下主窗口上的按钮创建新条目时,我首先实例化属性:Objective c 新NSManagedObject的Cocoa绑定,objective-c,cocoa,cocoa-bindings,Objective C,Cocoa,Cocoa Bindings,我是新来的,所以我会说明我在做什么,请纠正任何错误 在我的AppDelegate中,NSManagedObject子类Entry有一个@property。当我按下主窗口上的按钮创建新条目时,我首先实例化属性: _entry = (Entry*)[NSEntityDescription insertNewObjectForEntityForName:@"Entry" inManagedObjectContext:[se
_entry = (Entry*)[NSEntityDescription
insertNewObjectForEntityForName:@"Entry"
inManagedObjectContext:[self managedObjectContext]];
然后我打电话打开一个新窗口
[inputWindow makeKeyAndOrderFront:nil];
此inputWindow具有与条目属性的Cocoa绑定。当我提交时,我会做通常的保存
[[self managedObjectContext] save:&error];
然后
[inputWindow performClose:nil];
这看起来很直截了当,但我有一个错误:
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".
条目.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
很简单。在我的MainMenu.xib中,我有主窗口和inputWindow。我有一个绑定到managedObjectContext的数组控制器,它为主窗口上的表填充内容。我还有第二个窗口,用于向表中输入新条目。这是绑定到属性,条目
再次感谢你的帮助
Chet您的“Entry”类是否会覆盖NSManagedObject中的某些方法?甚至可能
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context;
如果是这样,您是否在子类的任何方法中都无法调用-super?在数组控制器中,我有一个模式:类引用入口类。我将其更改为模式:实体引用条目实体,然后一切都解决了: 有人能评论一下为什么调试器不善于给出有意义的响应吗?在错误消息中,哪里可以指出这是错误
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
我没有在Entry.m中编写任何方法。我已经注释掉了几乎所有的代码。我所拥有的只是AppDelegate中的一个/@属性和关联的/@synthesis,我仍然得到这个错误。我认为所发生的事情是,在实例化绑定对象之前,条目窗口立即被加载。我尝试在awakeFromNib中实例化实体,但没有成功。我真的不确定我在这里做什么。感谢您的帮助您是否尝试记录self.managedObjectContext以确保它不是零?你能发布你的Entry.h和Entry.m的剩余内容吗?删除注释掉的部分,这样我们就可以看到代码了?不知道在哪里登录self.managedObjectContext。我把它放在ApplicationIDFinishLaunching中,程序在启动前崩溃了。有关更多代码和问题解释,请参见我的编辑。
#import "Entry.h"
@implementation Entry
@dynamic amount;
@dynamic category;
@dynamic date;
@dynamic images;
@dynamic payee;
@dynamic text;
@end
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context;
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