• 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: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:


  • Subject: Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
  • From: Jim Correia <email@hidden>
  • Date: Wed, 15 Oct 2008 20:54:10 -0400

On Oct 15, 2008, at 8:46 PM, Chris Idou wrote:

I can't do that because my object inherits from NSObject, and NSObject doesn't contain an implementation of observeValueForKeyPath. So that gives a runtime error.

NSObject does have an implementation of - observeValueForKeyPath:ofObject:change:context: which raises an exception for all unknown contexts.


This has been discussed ad naseum on the list.

To properly use KVO you must use a unique context pointer, and implement your observer method like this:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == MY_CONTEXT) {
/* do my work here */
} else {
[super observeValueForKeyPath: keyPath ofObject: object change: change context: context];
}
}


Implement it like that always, even when you are a direct subclass of NSObject. (If you change your superclass, your implementation will still be correct. Etc.)

Jim
_______________________________________________

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


  • Follow-Ups:
    • Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
      • From: Chris Idou <email@hidden>
References: 
 >Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey: (From: Chris Idou <email@hidden>)

  • Prev by Date: Re: Objective-C parameter mutation
  • Next by Date: Re: -[NSMutableSet addObject:] Ambiguous Docs: -isEqual: vs. ==
  • Previous by thread: Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
  • Next by thread: Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
  • Index(es):
    • Date
    • Thread