Is this Key Value Compliant?
If I @synthesize my property (artist) everything works fine
- add observer for keyPath artist.name
- call [myObj setValue:newArtist forKey:@"artist"];
- Success.
if I override the setter method with this:
-开发者_JAVA百科 (void)setArtist:(GVArtist *)artist
{
GVArtist *oldArtist = _artist;
[self willChangeValueForKey:@"artist"];
_artist = [artist retain];
[self didChangeValueForKey:@"artist"];
[oldArtist release];
}
and do it again, I get:
Cannot update for observer for the key path "artist.name" from , most likely because the value for the key "artist" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the MyObject class.
It looks fine to me though....?
You don't need the willChangeValueForKey:
and didChangeValueForKey:
stuff; that's all handled automatically so long as your setter's name is KVO-compliant (which -setArtist:
is.) When you register an observer on myObj
, Cocoa dynamically makes a subclass of your object and adds the willChangeValueForKey:
and didChangeValueForKey:
calls automatically. So there's no need for your setter to do those manually, and it might be causing your problems.
精彩评论