I'm trying to understand how NSArrayController works...

  • When I create an NSMutableArray "employeeArray" to hold "Person" object. enter image description here
  • I use NSArrayController in GUI and bind with the "employeeArray". enter image description here
  • I add two buttons for adding and removing "Person" objects in "employeeArray". Both buttons are binded with the received action "add:" and "remove:" of NSArrayController. enter image description here
  • Inorder to do something else when adding/removing "Person" objects in "employeeArray", I add two methods:
    • (void)insertObject:(Person *)p inEmployeeArrayAtIndex:(NSUInteger)index;
    • (void)removeObjectFromEmployeeArrayAtIndex:(NSUInteger)index;

Question:

  1. I didn't do anything(write any code) to let NSArrayController know the two methods above, why they are called automatically when I click the add/remove button? What happened?
  2. What add/remove method does NSArrayController use?
  3. Does NSArrayController smart enough to recognized those methods? What if I modified the method names randomly, will it be called correctly?
有帮助吗?

解决方案

I think I finally understand:

By following "Key-Value Coding Accessor Methods Patterns". NSArrayController is smart enough to recognize the "insert" "remove" methods automatically.

If you alter the method name without following the accessor pattern. It will not work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top