“[copyWithZone:]:无法识别的选择器发送到实例”使用绑定/核心数据时

发布于 2024-11-19 12:10:51 字数 418 浏览 5 评论 0原文

(自我询问和自我回答,因为我花了几个小时在网上寻找这个,并且大多数资源都说“我最终解决了它”而没有给出解释)

我有一个非常简单的核心数据+绑定应用程序

  • : 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

反目相谮 2024-11-26 12:10:51

事实证明,上述所有内容都需要触发此操作,并且 XCode 允许您在 99.9%(即使不是 100%)的情况下执行错误的操作。

  1. 核心数据对象无法实现copyWithZone: - 这会导致崩溃

  2. 当使用 Bindings 填充 tableview 时,它会尝试复制 NSArrayController 中对象的以呈现每个列

  3. ...但是如果您无法完全指定列绑定(Xcode 允许您半指定它),则 tableview 会尝试“复制”在对象上,而不是它们的

我的绑定中的错误:我指定表列有一个“值”:

控制器键 =“arrangedObjects”
模型关键路径 =(空白)

(这是 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%.

  1. Core-Data objects cannot implement copyWithZone: - this causes the crash

  2. When a tableview is populated using Bindings, it tries to copy the values of the objects in the NSArrayController to render each Column

  3. ...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:

Controller Key = "arrangedObjects"
Model Key Path = (blank)

(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.:

Model Key Path = "value"

...and everything works again.

猫卆 2024-11-26 12:10:51

我还想在这里发帖,因为我对相同类型的

空定义对象也有类似的问题:

错误的代码:

@property (nonatomic, copy, readwrite) MSender * sender;

这应该会引发编译错误,因为使用引用计数 Xcode 不知道如何复制我的对象。相反,它在运行时失败...

编写代码:

@property (nonatomic, strong, readwrite) MSender * sender;

希望它可以帮助某人。

I would also like to post here because I had a similar issue with the same type of thing

empty defined object:

Wrong code:

@property (nonatomic, copy, readwrite) MSender * sender;

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:

@property (nonatomic, strong, readwrite) MSender * sender;

Hope it helps someone.

喜你已久 2024-11-26 12:10:51

感谢您提供这个解决方案,它为我指明了正确的方向。对于那些学习 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 my ArrayController.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.

南薇 2024-11-26 12:10:51

作为避免绑定出现任何问题的提示,可以将 NSManagedObject 转换为 NSDictionary,同时使用以下内容填充控制器:[objectdictionaryWithValuesForKeys:[[objectentity] ] attributeKeys]],连接和调试要容易得多:-)

顺便感谢您的指点!

As a hint to avoid any kind of problems with the binding one can convert the NSManagedObject into NSDictionary 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!

梦太阳 2024-11-26 12:10:51

当我尝试实现主从绑定时遇到此错误。类似于部门/员工。我尝试将 arrayController 绑定到 Department 的“selection.employees”并得到:

-[Employee copyWithZone:]:无法识别的选择器发送到实例

结果发现,员工关系没有按预期设置为一对多,而是一对一。一旦我纠正了这个问题,一切都很好。

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:

-[Employee copyWithZone:]: unrecognized selector sent to instance

Turned out the employees relationship was not set to to-many, as intended, but to-one. Once I corrected this, all was well.

相对绾红妆 2024-11-26 12:10:51

我也遇到了类似的问题,它来自于调用 ProxyObject 而不是来自数组控制器的实际对象。

例如:

MYEntity *entity = EntityController.selection;
NSString *property = entity.property;   // <--- causes the error

但是

MYEntity *entity = EntityController.selectedObjects firstObject]; // <--- fixes the error
NSString *property = entity.property;   

I also had a similar problem and it came from calling a ProxyObject rather than the actual object from an array controller.

For example:

MYEntity *entity = EntityController.selection;
NSString *property = entity.property;   // <--- causes the error

but

MYEntity *entity = EntityController.selectedObjects firstObject]; // <--- fixes the error
NSString *property = entity.property;   
草莓酥 2024-11-26 12:10:51

一不小心就遇到这个问题了。

经过一番调查后,我发现原因是在 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.

enter image description here

enter image description here

The problem was fixed by switch the binding off.

鲜肉鲜肉永远不皱 2024-11-26 12:10:51

当我从 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: in NSTableViewDelegate.)

翻身的咸鱼 2024-11-26 12:10:51

一般来说:检查您是否使用传递自定义参数而不是默认参数的 @objc 函数(选择器)

In general: Check if you are using a @objc function (selector) that passes custom parameters than the default ones

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文