cocoa - Is there a nice way to set up KVO on retained properties? -


quite encounter scenario when want observe changes on retained property:

@interface anobserver {…} @property(retain) foo *foo; 

now when want set observing, need write own setter, repeating boilerplate setter code:

- (void) setfoo: (foo*) newfoo {     if (newfoo == foo)         return;     [foo removeobserver:self forkeypath:…];     [foo release], foo = [newfoo retain];     [foo addobserver:self forkeypath:…]; } 

this dumb, because pollutes source boilerplate code , it’s easy miss something. there better way set kvo on retained properties? wish write moose’s after hook change kvo after property changed.

in fact realized watch property itself:

[self addobserver:self forkeypath:@"foo"…]; 

and change kvo when property changes :-), realize more complicated hand-written setter i’d avoid.

ideas?

how using key path? want observe changes on both value1 , value2 properties of foo. use:

[self addobserver:self forkeypath:@"foo.value1"]; [self addobserver:self forkeypath:@"foo.value2"]; 

then when properties change, you'll notifications.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -