objective c - Implementation of componentRGBA method called by KVC when setting a UIColor property -


i have class uicolor property named color , want set property string:

[label setvalue:@"1.0 0.5 0.0 1.0" forkey:@"color"]; 

i know need convert string uicolor. noticed kvc calls method named "componentrgba" want perform conversion. added category method on nsstring:

-(uicolor*) componentrgba {     cicolor* cicolor = [cicolor colorwithstring:self];     uicolor* uicolor = [uicolor colorwithcicolor:cicolor];     return uicolor; } 

the method called. however, self not seem valid nsstring object because call colorwithstring: crashes exc_bad_access , every attempt of sending self nsobject message (class, description, etc).

my suspicion method signature of componentrgba not correct , therefore self isn't string object. though not find reference googling method.

how implement componentrgba can perform color conversion automatically when uicolor property set nsstring* value via kvc?

update:

interestingly, when in componentrgba method:

cfshowstr((__bridge cfstringref)self); 

i receive message:

this nsstring, not cfstring

so it's supposed nsstring* yet can't call of methods without crashing.

this simple test example crashes:

nslog(@"self = %@", [self description]); 

the crash in objc_msgsend code=1 , address=0xffffffff (address varies time time).

in addition when don't implement componentrgba kvc fails following message:

-[__nscfconstantstring componentrgba]: unrecognized selector sent instance 0xc48f4 

this might of academic interest, don't want rely on undocumented method, following implementation seems work:

// structure returned (undocumened) componentrgba // method of uicolor. elements "float", on 64-bit, // cannot use cgfloat here. struct rgba {     float r, g, b, a; };  @interface uicolor (componentrgba) -(struct rgba) componentrgba; @end  @interface nsstring (componentrgba) -(struct rgba) componentrgba; @end  @implementation nsstring (componentrgba) -(struct rgba) componentrgba {     cicolor* cicolor = [cicolor colorwithstring:self];     uicolor* uicolor = [uicolor colorwithcicolor:cicolor];     return [uicolor componentrgba]; } @end 

i figured out of sample project of (now deleted) question kvc: 'componentrgba' method when setting color property?. key point (as 1 see inspecting stack backtrace) componentrgba method called via objc_msgsend_stret(), means returns struct , not id.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -