Search code examples
objective-ccocoatypedefcocoa-bindings

Why cocoa binding won't work with typedef of the framework class


Why there is an error, when trying to bind NSColorWell value to some property...

valueForUndefinedKey: this class is not key value coding-compliant for the key color.

If color property is defined as:

 @property (strong, nonatomic) MyColor *color;

and MyColor is defined as:

typedef NSColor MyColor;

But everything works OK when color property is defined as a NSColor?


Solution

  • You appear to have found a bug, as typedef's should be transparent and not change behaviour.

    Testing shows that with:

    typedef AnyOldClass SomeOldClass;
    

    and properties in different classes:

    @property AnyOldClass *anyclass; // from class A
    
    @property SomeOldClass *someclass; // from class B
    

    then trying to bind the to with bind:toObject:withKeyPath:options: fails. It does appear to work (i.e. non-exhaustive testing) if the typedef is for a value type (e.g. double).

    Furthermore it can work if it is changed to:

    typedef AnyOldClass *SomeOldClass;
    

    and

    @property SomeOldClass someclass; // from class B
    

    that is the typedef is for the pointer type itself. However not the emphasis on can, it doesn't not seem to be consistent between compiles - if it works for a binary it always works for that binary, but a recompilation may break it.

    I haven't examined the metadata (at least yet...)

    Weird and intriguing. Report it to Apple unless someone jumps in with an explanation soon.

    Testing done on Xcode 5.0.2/Clang 4.2