“[copyWithZone:]:无法识别的选择器发送到实例”使用绑定/核心数据时
(自我询问和自我回答,因为我花了几个小时在网上寻找这个,并且大多数资源都说“我最终解决了它”而没有给出解释)
我有一个非常简单的核心数据+绑定应用程序
- : NSArrayController 从核心数据中提取项目
- NSTableView 渲染它们
- 另一个 NSTableView,当您单击第一个表中的行时,显示该项目的详细信息
上面的项目 3 导致应用程序崩溃,并出现错误:
[(my NSManagedObject) copyWithZone:]: unrecognized selector sent to instance
实现该方法(!)并且在那里放置一个断点,我发现它被 Apple 的 NSCell 类调用 - 这没有多大帮助:(。
(self asking and self-answering because I spent hours on the web looking for this, and most of the resources all say "I solved it in the end" without giving an explanation)
I had a very simple Core Data + Bindings application:
- An NSArrayController pulling items out of Core Data
- An NSTableView rendering them
- Another NSTableView that when you click on a row in the first table, displays the details of that item
Item 3 above was causing application crash, with the error:
[(my NSManagedObject) copyWithZone:]: unrecognized selector sent to instance
Implementing that method (!) and putting a breakpoint there, I found it was being invoked by Apple's NSCell class - this didn't much help :(.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
事实证明,上述所有内容都需要触发此操作,并且 XCode 允许您在 99.9%(即使不是 100%)的情况下执行错误的操作。
核心数据对象无法实现copyWithZone: - 这会导致崩溃
当使用 Bindings 填充 tableview 时,它会尝试复制 NSArrayController 中对象的值以呈现每个列
...但是如果您无法完全指定列绑定(Xcode 允许您半指定它),则 tableview 会尝试“复制”在对象上,而不是它们的值
我的绑定中的错误:我指定表列有一个“值”:
(这是 XCode4 自动完成中的一个错误 - 有时当您太快地离开时,它会删除 ModelKeyPath 字段)
完成绑定中的输入,例如:
......一切都再次正常。
Turns out ALL the above are needed to trigger this, and XCode is allowing you to do something that's wrong in 99.9% of situations, if not 100%.
Core-Data objects cannot implement copyWithZone: - this causes the crash
When a tableview is populated using Bindings, it tries to copy the values of the objects in the NSArrayController to render each Column
...but if you fail to specify the Column binding fully (Xcode allows you to half specify it), then tableview tries the "copy" on the objects instead of their values
The bug in my bindings: I had specified that a Table Column had a "value" with:
(this is a bug in XCode4 autocomplete - it will delete the ModelKeyPath field sometimes when you tab away too quickly)
Finish typing-in the binding, e.g.:
...and everything works again.
我还想在这里发帖,因为我对相同类型的
空定义对象也有类似的问题:
错误的代码:
这应该会引发编译错误,因为使用引用计数 Xcode 不知道如何复制我的对象。相反,它在运行时失败...
编写代码:
希望它可以帮助某人。
I would also like to post here because I had a similar issue with the same type of thing
empty defined object:
Wrong code:
this should trow a compile error because with reference counting Xcode has no idea how to copy my object. Instead it fails at run time...
Write code:
Hope it helps someone.
感谢您提供这个解决方案,它为我指明了正确的方向。对于那些学习 InterfaceBuilder 的人,我希望这些附加信息有所帮助。
事实证明,在构建教程时,我无意中将
文本字段单元格 - 文本单元格
绑定到我的ArrayController.ObjectValue
。真正的绑定应该发生在
Table Column > 处。表格单元格视图>静态文本 - 表视图单元格
这个是正确的,但在视觉上位于它下面的树中(由于我还不明白的原因,IB 需要一个
文本字段单元格
。)我有 也绑定:表格列>文本字段单元格 - 文本单元格
这是第二个绑定尝试复制整个对象,因为路径是没有键的
objectValue
,它触发了此错误。可能是一个新手错误,但这意味着遍历每个对象并检查绑定,我遇到了这个一个。
Thank you for this solution, it pointed me in the right direction. For those of you learning InterfaceBuilder I hope this additional information helps.
It turns out while building a tutorial I had inadvertently Bound a
Text field Cell - Text Cell
to myArrayController.ObjectValue
.The real binding was supposed to happen at
Table Column > Table Cell View > Static Text - Table View Cell
That one was correct, but visually in the tree below it (for reasons I don't understand yet IB needs a
Text Field Cell
.) I had also bound:Table Column > Text Field Cell - Text Cell
It was that second binding which trying to copy the whole object because the path was
objectValue
with no key, it was triggering this error.Probably a newbie mistake, but it meant walking through EVERY object and checking for bindings and I came across this one.
作为避免绑定出现任何问题的提示,可以将
NSManagedObject
转换为NSDictionary
,同时使用以下内容填充控制器:[objectdictionaryWithValuesForKeys:[[objectentity] ] attributeKeys]]
,连接和调试要容易得多:-)顺便感谢您的指点!
As a hint to avoid any kind of problems with the binding one can convert the
NSManagedObject
intoNSDictionary
while populating the controllers with:[object dictionaryWithValuesForKeys:[[object entity] attributeKeys]]
, it is a lot easier to connect and debug :-)Thanks for the pointer by the way!
当我尝试实现主从绑定时遇到此错误。类似于部门/员工。我尝试将 arrayController 绑定到 Department 的“selection.employees”并得到:
结果发现,员工关系没有按预期设置为一对多,而是一对一。一旦我纠正了这个问题,一切都很好。
I encountered this error when I tried to implement a master-detail binding. Something analogous to Department / Employee. I tried to bind an arrayController to "selection.employees" of Department and got:
Turned out the employees relationship was not set to to-many, as intended, but to-one. Once I corrected this, all was well.
我也遇到了类似的问题,它来自于调用 ProxyObject 而不是来自数组控制器的实际对象。
例如:
但是
I also had a similar problem and it came from calling a ProxyObject rather than the actual object from an array controller.
For example:
but
一不小心就遇到这个问题了。
经过一番调查后,我发现原因是在
NSTextField
-cell 对象上设置了错误的可可绑定选项。通过关闭绑定解决了该问题。
Accidentally faced with this issue.
After a little investigation I found out that the reason was wrong cocoa binding option set on
NSTextField
-cell object.The problem was fixed by switch the binding off.
当我从
NSTableViewDataSource
函数-tableView:objectValueForTableColumn:row:
返回视图时,我得到了这个。相反,它应该返回一个值(例如NSString*
)。 (鉴于其名称,这并不奇怪,但该错误对发现这一点没有帮助。我想到的函数是NSTableViewDelegate
中的-tableView:viewForTableColumn:row:
。)I got this when I returned a view from the
NSTableViewDataSource
function-tableView:objectValueForTableColumn:row:
. Instead, it is supposed to return a value (e.g.NSString*
). (Not surprising given its name, but the error is not helpful in discovering this. The function I was thinking of is-tableView:viewForTableColumn:row:
inNSTableViewDelegate
.)一般来说:检查您是否使用传递自定义参数而不是默认参数的
@objc
函数(选择器)In general: Check if you are using a
@objc
function (selector) that passes custom parameters than the default ones