Re: adding something to a setter
Re: adding something to a setter
- Subject: Re: adding something to a setter
- From: Jim Correia <email@hidden>
- Date: Thu, 06 Oct 2011 19:30:04 -0700
Do not use self as your observation context.
Your observation context has to be unique for all observers of the object. “self” is a bad choice.
Also, you should only do your own work in the case that the observer is your observer. In the code below, you are also doing your work in the case where you should only be calling through to super.
—Jim
On Oct 6, 2011, at 5:47 AM, Stephan Michels wrote:
> I use KVO to execute custom code if a property has changed.
>
> [self addObserver:self forKeyPath:@"graphics" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:self];
>
> - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
> if (context != self) {
> [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
> }
>
> if ([keyPath isEqualToString:@"graphics"]) {
> [self setNeedsDisplay:YES];
> }
> }
>
> AFAIK the observe method will not be executed instantaneously, but on the other side setNeedsDisplay has also a
> deferred execution.
>
> Stephan.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden