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

前端 未结 8 1622
情深已故
情深已故 2020-12-28 13:49

(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

相关标签:
8条回答
  • 2020-12-28 14:08

    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!

    0 讨论(0)
  • 2020-12-28 14:10

    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.

    0 讨论(0)
  • 2020-12-28 14:11

    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.

    0 讨论(0)
  • 2020-12-28 14:18

    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.

    0 讨论(0)
  • 2020-12-28 14:24

    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.

    0 讨论(0)
  • 2020-12-28 14:27

    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;   
    
    0 讨论(0)
提交回复
热议问题