Passing CGColorRef as a value in KVC/KVO?

I was going to suggest using NSValue to wrap your color object, but during testing I have come to the conclusion that KVO/KVC will not work with CGColors. According to the Key-Value Coding Programming Guide primitive types and structures are wrapped using NSValue or NSNumber. However, when trying to access a CGColorRef property, I get a "not KVC compliant" exception.It seems that pointers to structures are not wrapped to NSValues in the same way as the structures themselves.

Here is my test code.

I was going to suggest using NSValue to wrap your color object, but during testing I have come to the conclusion that KVO/KVC will not work with CGColors. According to the Key-Value Coding Programming Guide, primitive types and structures are wrapped using NSValue or NSNumber. However, when trying to access a CGColorRef property, I get a "not KVC compliant" exception.It seems that pointers to structures are not wrapped to NSValues in the same way as the structures themselves.

Here is my test code: @interface MyClass : NSObject { CGColorRef color; } @property (assign) CGColorRef color; @end @implementation MyClass - (CGColorRef)color { return color; } - (void)setColor:(CGColorRef)newColor { color = newColor; } @end int main(int argc, char **argv) { NSAutoreleasePool *p = NSAutoreleasePool new; MyClass *theObject = MyClass new; CGColorRef theColor = CGColorCreateGenericRGB(1.0,1.0,1.0,1.0); theObject. Color = theColor; // works NSLog(@"%@",theObject valueForKey:@"color"); // NSValue expected, actually causes exception p release; return 0; } One possible workaround is to add a category to the proper class which defines accessors for a different key. These accessors would be NSValue wrappers for the numColour key.

// in the category - (NSValue *)numColourKVC { return NSValue valueWithPointer:(void*)self. NumColour; } - (void)setNumColourKVC:(NSValue *)newValue { self. NumColour = (CGColorRef)newValue pointerValue; } Then you would set the color in your testbed as follows: myCALayerReceiverObject setValue:NSValue valueWithPointer:(void*)color forKey:@"numColourKVC".

I just noticed, CGColorCreateGenericRGB isn't valid for iOS, so my test code will only work for OS X. It doesn't really matter where the color comes from since it isn't the problem. – ughoavgfhw May 8 at 22:12 Thank you for this ughoavgfhw.

Since posting the question, I've been in communication with Opacity and the reason my code was failing at all is down to a small omission in the Opacity logic that creates the CALayer subclasses. In fact, what I was trying to do originally is correct and the Opacity developer will tackle the omission in a future release of the poduct. Thanks for your help.

– VectorVictor May 9 at 12:52.

The error is fairly self-explanatory - you need to cast your CGColorRef as (id). Off the top of my head, this should work: myCALayerReceiverObject setValue: (id)color forKey: @"numColour".

1 To say the error is 'self-explanatory' is not helpful. Which is a shame, since your answer was ultimately helpful and correct. – imnk Dec 19 at 23:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions