• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: adding something to a setter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >adding something to a setter (From: Torsten Curdt <email@hidden>)
 >Re: adding something to a setter (From: Stephan Michels <email@hidden>)

  • Prev by Date: -[NSURLCredentialStorage removeCredential:forProtectionSpace:] broken?
  • Next by Date: Re: NSPrintInfo and NSPageLayout
  • Previous by thread: Re: adding something to a setter
  • Next by thread: Re: adding something to a setter
  • Index(es):
    • Date
    • Thread