Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
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