2
votes

If I @synthesize my property (artist) everything works fine

  1. add observer for keyPath artist.name
  2. call [myObj setValue:newArtist forKey:@"artist"];
  3. Success.

if I override the setter method with this:

- (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....?

1

1 Answers

4
votes

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.