0

I must be missing something. I'm in the chapter titled "Key-Value Observing" in Cocoa Programming by Aaron Hillegass.

I have inserted the code that enables the application to undo/redo adding and subtracting employees from RaiseMan. The application works however what I'm wondering is why is that when I link the "Add Employee" to the NSArrayController to the ADD method (using the .xib file) it calls

- (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index;

According to the Key-Value Coding, shouldn't the add method call?

- (void)addEmployeesObject:newEmployee;

I'm linking the 'add' method not the 'insert' method.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
schmudu
  • 2,111
  • 1
  • 21
  • 30

2 Answers2

1

This is expected behavior when using KVC collection accessor methods. It's more efficient to insert an object at the desired location (even if that location is at the end) than to wonder if "it's at the end" and call -add... directly. All of this is much more efficient than, say, replacing the entire array with an entirely new (-setEmployees:, for example) array when the range of the modification is already known. NSArrayController ultimately uses this method when inserting an object into the array it's controlling.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
1

Your array controller is bound to an ordered collection (an array). That's why it uses insertObject:inEmployeesAtIndex: to add a new object at the end of the the collection.

The addEmployeesObject: method would be used if the collection was unordered (i.e. a set).

Nicolas Bachschmidt
  • 6,475
  • 2
  • 26
  • 36