views:

329

answers:

2

I was wondering if it is possible at run-time to dynamically add new properties to an Objective-C object instance?

My initial thought would just to overrride the getValueForKey to "fake" a property but it seems like this doesn't work with CoreAnimation. What I want to achieve is to be able to animate custom properties. I have been able to get that to work if I create a subclass of CALayer and add declared properties to my subclass. If I try to use the getValueForKey/setValueForKey strategy it seems like CoreAnimation doesn't care for that and it is explicitly looking for declared properties.

I would like to be able to dynamically add the properties because I might not know what property I want to animate until runtime. I can of course create a CALayer subclass that has all the properties that I would ever want to animate...but just wondering if there is a nicer way to do this...

Thanks,

Peter

+2  A: 

Have you tried overriding valueForUndefinedKey: instead? (I do this on a custom NSObject subclass that can have various properties whose names are pulled from a database.)

Wevah
+1  A: 

You could override -respondsToSelector: and -doesNotUnderstand: to process incoming messages dynamically if need be.

Jeffrey Hantin