How does NSArrayController know?

  • The following quote is from chapter 7 of Aaron's book.

    "You plan to store the array inside MyDocument and to set the NSArrayController's contentArray binding so as to tell it to use MyDocument's array as its content. You will also write two methods that will be called when NSArrayController wishes to insert or remove a Person object.

    Open MyDocument.h and add two instance variables and two actions:

    @interface MyDocument : NSDocument
    {
        IBOutlet NSArrayController *personController;
                    NSMutableArray *employees;
    }
    - (void)insertObject:(Person *)p inEmployeesAtIndex:(int)index;
    - (void)removeObjectFromEmployeesAtIndex:(int)index;
    - (void)setEmployees:(NSMutableArray *)array
    @end

    "
    Later he says "the two methods in MyDocument  will be called automatically to insert and remove Person objects from the employees array."

    My question is how the NSArrayController knows the method names insertObject:inEmployeesAtIndex: and removeObjectFromEmployeesAtIndex: (the word employees is chosen by our app and the general class NSArrayController can't possible know that).

    I've looked at "NSArrayController Class Reference" but couldn't find any info. I suspected  kvc compliance but the document never mentions it (or I missed it).

    Bob

          ____________________________________________________________________________________
    Don't let your dream ride pass you by. Make it a reality with Yahoo! Autos.
    http://autos.yahoo.com/index.html
  • On Oct 5, 2007, at 12:03 AM, Bob Ueland wrote:

    > My question is how the NSArrayController knows the method names
    > insertObject:inEmployeesAtIndex: and
    > removeObjectFromEmployeesAtIndex: (the word employees is chosen by
    > our app and the general class NSArrayController can't possible know
    > that).
    >
    > I've looked at "NSArrayController Class Reference" but couldn't
    > find any info. I suspected  kvc compliance but the document never
    > mentions it (or I missed it).
    >
    > Bob

    You're right, it's KVC compliance for the mutable part of a to-many
    relationship named 'Employees'.

    You can find the details on this page:
    http://developer.apple.com/documentation/Cocoa/Conceptual/
    KeyValueCoding/Concepts/AccessorConventions.html

    Or it's nicely summarized here:
    http://developer.apple.com/documentation/Cocoa/Conceptual/
    KeyValueCoding/Concepts/Compliant.html
  • On Oct 5, 2007, at 12:03 AM, Bob Ueland wrote:

    > I've looked at "NSArrayController Class Reference" but couldn't find
    > any info. I suspected  kvc compliance but the document never
    > mentions it (or I missed it).

    You suspect correctly; in the example, MyDocument is Key-Value Coding
    compliant for an ordered relationship under the "employees" key.  To
    get at this ordered relationship, the interested party can either
    invoke the methods directly or (in generic, reusable code like
    NSArrayController) manipulate the proxy retuned as the result of
    sending an instance of MyDocument -mutableArrayValueForKey: with the
    key "employees".

    How this works is documented in the Programming Guide for Key-Value
    Coding as well as in <Foundation/NSKeyValueCoding.h>.  There are a
    number of additional methods that can be implemented that will also be
    invoked at appropriate times.  It's also fundamental to working with
    Core Data relationships, though they are unordered and thus use -
    mutableSetValueForKey:.

      -- Chris
  • Jeffrey J. Early wrote

    it's nicely summarized here:
    http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Co
    ncepts/Compliant.html


    Thank you Chris and Jeffrey

    The linked document provided the answer I was looking for

    If the to-many related objects are mutable, and the -<key> method does not return an NSMutableArray, you must also implement -insertObject:in<Key>AtIndex: and -removeObjectFrom<Key>AtIndex:.

          ____________________________________________________________________________________
    Shape Yahoo! in your own image.  Join our Network Research Panel today!  http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today