Quite often I encounter a scenario when I want to observe changes on a retained property:
@interface AnObserver {…}
@property(retain) Foo *foo;
Now when I want to set up the observing, I need to write my own setter, repeating all the 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 is dumb, because it pollutes the source with boilerplate code and it’s easy to miss something. Is there a better way to set up KVO on retained properties? I wish I could write something like Moose’s after
hook to change the KVO after the property was changed.
In fact I realized I could watch the property itself:
[self addObserver:self forKeyPath:@"foo"…];
And then change the KVO when the property changes :-), but I do realize this is much more complicated than the hand-written setter I’d like to avoid.
Ideas?