• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
why does it matter where I get the mutableArrayForKey:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

why does it matter where I get the mutableArrayForKey:


  • Subject: why does it matter where I get the mutableArrayForKey:
  • From: Roland King <email@hidden>
  • Date: Sat, 02 Aug 2008 18:21:46 +0800

Still struggling with the nuts and bolts of KVC. I understood eventually that just observing a property (test) which was an NSMutableArray and then mutating it with removeObjectAtIndex: isn't KVC compliant, didn't give the observers callbacks, and implemented the removeObjectFromTestAtIndex: method. Then I finally figured out that I can either call that method directly, or I can get mutableArrayValueForKey:@"test" and use the normal array operations on that proxy as it's KVC compliant.

However I failed to make it work until I moved the getter, mutableArrayValueForKey:@"test" *after* the line I where set up my KVO observation. If I get this mutable array beforehand, I don't get the notification. Why does it matter when I get the mutableArrayValueForKey:?

As the code is set up below, I won't get a KVO callback for [ test2 removeObjectAtIndex:0 ], if however you comment out the 'place one' line and uncomment the 'place two' line, you will get the callback. In both cases [ test2 removeObjectAtIndex:0 ] does call the removeObjectFromTestAtIndex: method, so I would expect notificiation.

Bar *bar = [ [ Bar alloc ] init ]; // bar has one property, test, an NSMutableArray and it has the KVC mutator methods defined.
Baz *baz = [ [ Baz alloc ] init ]; // baz has nothing but a logging observeValueForKeyPath: method



NSMutableArray *test2 = [ bar mutableArrayValueForKey:@"test" ]; // place one


	[ bar addObserver:baz forKeyPath:@"test" options:0 context:nil ];

//NSMutableArray *test2 = [ bar mutableArrayValueForKey:@"test" ]; // place two

// only get a callback on this one if I get the mutableArrayValueForKey: in place two, after I've added the observer.
[ test2 removeObjectAtIndex:0 ];

// always get a KVO callback for this one
[ bar removeObjectFromTestAtIndex:0 ];

_______________________________________________


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


  • Prev by Date: Re: My own listbox
  • Next by Date: Re: My own listbox
  • Previous by thread: Re: My own listbox
  • Next by thread: Non-editable TableView and the Evil NSEditorBinder
  • Index(es):
    • Date
    • Thread