Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"[something copyWithZone:]: unrecognized selector sent to instance" when using Bindings / Core Data

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

like image 415
Adam Avatar asked Jul 10 '11 23:07

Adam


2 Answers

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.

like image 196
Adam Avatar answered Sep 18 '22 12:09

Adam


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.

like image 31
GoodStuff Avatar answered Sep 18 '22 12:09

GoodStuff