我有一个简单的Core Data应用程序,用于建模对象:员工和笔记。员工可以有很多笔记(1:很多),每个笔记属于一个员工(1:1)。在员工窗口中,我使用NSArrayController显示文件中的员工列表(NSTableView)。单击员工会在列表旁边显示其详细信息(名称地址,性别等)。现在,我还想显示所选员工的文件备注列表(也是NSTableView)。该列表应显示文件中每个注释的2个属性:其主题及其输入日期)。
我尝试执行以下操作:(1)将表的日期列绑定到Employee控制器,控制器键:选择,模型键路径:notes.date(2)绑定Employee控制器的主题列,控制器key:选择,模型关键路径:notes.topic
然而,这会引发异常(无法识别的选择器)。
如何将员工的notes属性正确绑定到NSTableView?可以使用IB中的cocoa绑定“直接”完成,还是在设置绑定之前访问员工的notes属性所需的其他代码?我检查了Core Data写的数据,看起来很不错。
非常感谢你的帮助......
编辑:这是来自调试器的堆栈跟踪...
2011-09-30 00:46:56.010 EmployeeDemo[3843:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSFaultingMutableSet objectAtIndex:]: unrecognized selector sent to instance 0x10260b9c0' *** First throw call stack: ( 0 CoreFoundation 0x00007fff8c654986 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff8b040d5e objc_exception_throw + 43 2 CoreFoundation 0x00007fff8c6e05ae -[NSObject doesNotRecognizeSelector:] + 190 3 CoreFoundation 0x00007fff8c641803 ___forwarding___ + 371 4 CoreFoundation 0x00007fff8c641618 _CF_forwarding_prep_0 + 232 5 AppKit 0x00007fff87c72648 -[NSTableBinder _visibleRowIndexesForObject:] + 267 6 AppKit 0x00007fff87c72291 -[NSTableBinder observeValueForKeyPath:ofObject:change:context:] + 131 7 Foundation 0x00007fff8dbb818a NSKeyValueNotifyObserver + 387 8 Foundation 0x00007fff8dbb21af -[NSObject(NSKeyValueObservingPrivate) _notifyObserversForKeyPath:change:] + 756 9 AppKit 0x00007fff876f1f0b -[NSController _notifyObserversForKeyPath:change:] + 206 10 AppKit 0x00007fff877fc5fc -[NSController observeValueForKeyPath:ofObject:change:context:] + 847 11 AppKit 0x00007fff879a1cf9 -[NSArrayController observeValueForKeyPath:ofObject:change:context:] + 98 12 Foundation 0x00007fff8dbb818a NSKeyValueNotifyObserver + 387 13 Foundation 0x00007fff8dbd9387 NSKeyValueDidChange + 486 14 Foundation 0x00007fff8dc6834b -[NSObject(NSKeyValueObservingPrivate) _didChangeValuesForKeys:] + 631 15 CoreData 0x00007fff8e827543 _PFFaultHandlerFulfillFault + 3619 16 CoreData 0x00007fff8e825bd8 _PFFaultHandlerLookupRow + 1592 17 CoreData 0x00007fff8e825254 _PF_FulfillDeferredFault + 212 18 CoreData 0x00007fff8e8250d8 _sharedIMPL_pvfk_core + 56 19 CoreData 0x00007fff8e85bd3e -[NSManagedObject valueForKey:] + 222 20 Foundation 0x00007fff8dbb479e -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 348 21 AppKit 0x00007fff8799ded5 -[NSArrayController _multipleValueForKeyPath:atIndex:] + 84 22 AppKit 0x00007fff8799cfc9 -[NSArrayController _singleValueForKeyPath:] + 151 23 AppKit 0x00007fff87704cfe -[_NSControllerObjectProxy valueForKeyPath:] + 77 24 Foundation 0x00007fff8dbb4761 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 287 25 AppKit 0x00007fff87704baa -[NSBinder _valueForKeyPath:ofObject:mode:raisesForNotApplicableKeys:] + 654 26 AppKit 0x00007fff87704894 -[NSBinder valueForBinding:resolveMarkersToPlaceholders:] + 171 27 AppKit 0x00007fff87907352 -[NSValueBinder _referenceBindingValue] + 31 28 AppKit 0x00007fff87907163 -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 647 29 AppKit 0x00007fff87906e48 -[NSValueBinder _observeValueForKeyPath:ofObject:context:] + 303 30 AppKit 0x00007fff8791ed0b -[NSTextValueBinder _observeValueForKeyPath:ofObject:context:] + 43 31 Foundation 0x00007fff8dbb818a NSKeyValueNotifyObserver + 387 32 Foundation 0x00007fff8dbb21af -[NSObject(NSKeyValueObservingPrivate) _notifyObserversForKeyPath:change:] + 756 33 AppKit 0x00007fff876f1f0b -[NSController _notifyObserversForKeyPath:change:] + 206 34 AppKit 0x00007fff8795e7a5 -[NSArrayController didChangeValuesForArrangedKeys:objectKeys:indexKeys:] + 120 35 AppKit 0x00007fff879601bd -[NSArrayController setContent:] + 870 36 AppKit 0x00007fff879a2664 -[NSArrayController(NSManagedController) _performFetchWithRequest:merge:error:] + 370 37 AppKit 0x00007fff87bb149e -[NSObjectController(NSManagedController) fetchWithRequest:merge:error:] + 177 38 AppKit 0x00007fff87bb1522 -[NSObjectController(NSManagedController) _executeFetch:didCommitSuccessfully:actionSender:] + 92 39 AppKit 0x00007fff87d932e7 _NSSendCommitEditingSelector + 30 40 AppKit 0x00007fff87a50246 -[NSController _controllerEditor:didCommit:contextInfo:] + 188 41 libdispatch.dylib 0x00007fff847df90a _dispatch_call_block_and_release + 18 42 libdispatch.dylib 0x00007fff847e177a _dispatch_main_queue_callback_4CF + 308 43 CoreFoundation 0x00007fff8c5e9c0c __CFRunLoopRun + 1724 44 CoreFoundation 0x00007fff8c5e9216 CFRunLoopRunSpecific + 230 45 HIToolbox 0x00007fff871924ff RunCurrentEventLoopInMode + 277 46 HIToolbox 0x00007fff87199c21 ReceiveNextEventCommon + 355 47 HIToolbox 0x00007fff87199aae BlockUntilNextEventMatchingListInMode + 62 48 AppKit 0x00007fff876de191 _DPSNextEvent + 659 49 AppKit 0x00007fff876dda95 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 135 50 AppKit 0x00007fff876da3d6 -[NSApplication run] + 463 51 AppKit 0x00007fff8795852a NSApplicationMain + 867 52 EmployeeDemo 0x0000000100001a62 main + 34 53 EmployeeDemo 0x0000000100001a34 start + 52 ) terminate called throwing an exception
编辑2:
似乎还有另一个与底层模型有关的问题。员工的注释通过带有自己的控制器的单独窗口输入。它创建新笔记并允许用户设置其属性(主题,正文)。当用户按下“保存”按钮时,执行saveNote方法:
- (IBAction)saveNote:(id)sender {
[[self note] setValue: [[self topicField] stringValue] forKey: @"topic"];
[[self note] setValue: [[self textView] string] forKey: @"bodyText"];
[[self note] setValue: [self employee] forKey: @"employee"];
NSError *error = nil;
if (![[self managedObjectContext] save:&error]) {
//todo: add proper error handling
NSLog(@"An error occurred while trying to save the memo");
}
[[self window] close];
}
当我逐步执行此方法时,实际上在设置笔记与员工(第3行)之间的关系时会生成异常。
答案 0 :(得分:2)
您需要的是一个NSArrayController,其内容绑定到该关系。然后将NSTableView内容绑定到NSArrayController。从那里你可以做列。
更新
这tutorial from Apple可能会帮助你。
答案 1 :(得分:0)
我的问题中描述的行为似乎与拥有基于文档的Core Date应用程序或具有“普通”核心数据应用程序(在XCode中创建新项目时的检查)有关。
我发现当我选择基于文档的核心数据应用程序时,不会出现上述问题。当我开发与非文档核心数据应用程序相同的应用程序时,绑定Notes控制器会生成堆栈跟踪,如我原始问题中所示,并且只要我向现有员工添加注释,就会抛出异常。
所以,公平地说,这只是解决方案的一部分,因为我对Cocoa和XCode的体验不足以解释为什么它不能使用非基于文档的变体。