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

Adam picture Adam · Jul 11, 2011 · Viewed 16k times · Source

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

Answer

Adam picture Adam · Jul 11, 2011

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.