• 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
Re: KVC and addObject for "to-many" properties
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: KVC and addObject for "to-many" properties


  • Subject: Re: KVC and addObject for "to-many" properties
  • From: Quincey Morris <email@hidden>
  • Date: Sat, 11 Jul 2009 15:32:22 -0700

On Jul 11, 2009, at 14:36, Michael de Haan wrote:

There have been quite a few questions about "RaiseMan" in the archives, but none have addressed this and the documentation that I have read does not answer this either, that I can find. :-). After implementing Undo/Redo in the app, where, in a nutshell, the contentArray of an ArrayController is bound to an NSMutableArray of the model. In order to implement UndoRedo, we make use of the "key- value coding to add and remove" (per Hillegass) objects of the Array.

The documentation is clear as to which methods one needs to implement to comply with KVC , but the one that is missing is a simple "addObject", which I assume one is doing when the user clicks the "Create New Employee" button.

The only two methods implemented by Hillegass are the

-insertObject:in<Key>AtIndex:index;
- (void)removeObjectFrom<Key>AtIndex:(NSUInteger)index;

In order to do this then, does Cocoa simply obtain a count of the array( behind the scenes, so to say, even if this method is not implemented), then use

-insertObject:in<Key>AtIndex:Count ?

Here's how it works: The NSArray class implements everything but the 5 primitive methods.


addObject: is non-primitive, so NSArray's implementation calls insertObject:atIndex:self.count on your behalf. That behavior comes for free for every NSArray subclass (although any subclass is permitted to override addObject: if it wants to do something different).

When the NSArray subclass is a mutable array proxy (which is the subclass that the array controller uses to get KVO compliance), the proxy's implementation of insertObject:atIndex: calls insertObject:in<Key>AtIndex: if it can find it. (Conceptually, that's what it does. What it actually does we don't know.)

That means that addObject: ends up at your insertObject:in<Key>AtIndex: without your having to write any code.

In NSArrayController's add: or addObject: method, one of which may well be invoked in the button's action, the array controller ends up calling addObject: (or possibly insertObject:atIndex:, we don't really know) for a mutable array proxy for your array property. Whichever one it calls, it gets routed as described above.

The longest invocation chain is probably something like this:

[NSArrayController add:]-->
[NSArrayController addObject:]-->
[NSArray addObject:]-->
["NSMutableArrayProxy" insertObject:atIndex:]-->
["Your" insertObject:in<Key>AtIndex:]-->
[NSArray insertObject:atIndex:]

Clear as mud?


_______________________________________________

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


  • Follow-Ups:
    • Re: KVC and addObject for "to-many" properties
      • From: Michael de Haan <email@hidden>
References: 
 >KVC and addObject for "to-many" properties (From: Michael de Haan <email@hidden>)

  • Prev by Date: Re: KVC and addObject for "to-many" properties
  • Next by Date: Re: NSScrollView
  • Previous by thread: Re: KVC and addObject for "to-many" properties
  • Next by thread: Re: KVC and addObject for "to-many" properties
  • Index(es):
    • Date
    • Thread