开发者

Is this Key Value Compliant?

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:

-开发者_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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜