Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
- Subject: Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
- From: Chris Idou <email@hidden>
- Date: Wed, 15 Oct 2008 19:12:59 -0700 (PDT)
Thanks, that's good to know. But it doesn't solve my particular problem. I'm now calling the super class as described, but it doesn't help.
int foo;
int *bar = &foo;
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSLog(@"oVFKP: %@", keyPath);
if (context == bar) {
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
--- On Wed, 10/15/08, Jim Correia <email@hidden> wrote:
> From: Jim Correia <email@hidden>
> Subject: Re: tearing my hair out: +(NSSet *)keyPathsForValuesAffectingValueForKey:
> To: email@hidden
> Cc: "Cocoa Developers" <email@hidden>
> Date: Wednesday, October 15, 2008, 5:54 PM
> 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