When to use key-value coding to get values vs. accessors

  • Hi-

    Not looking for an in-depth explanation (I should be able to find/
    understand the docs once I get tipped in the right direction), but
    can someone briefly hint to me the reason Apple uses key-value coding
    in this example code:

    - (NSString *)fullNameAndID
    {
        return [NSString stringWithFormat:@"%@, %@ (%@)",
                [self valueForKey:@"lastName"],
                [self valueForKey:@"firstName"],
                [self valueForKey:@"employeeID"]];
    }

    the example comes from
    http://developer.apple.com/documentation/Cocoa/Conceptual/
    NSPersistentDocumentTutorial/03_CustomClass/chapter_4_section_3.html

    Is there any advantage over doing it how I would tend to think to do
    it?:

    - (NSString *)fullNameAndID
    {
        return [NSString stringWithFormat:@"%@, %@ (%@)",
                [self lastName],
                [self firstName],
                [self employeeID]];
    }

    It must be because using -valueForKey: triggers something somewhere
    but I don't quite have a handle on what. Is it better Cocoa practice
    to just always use valueForKey? Or do I have to keep track in my head
    on an object by object (or even accessor by accessor) basis when it
    should be used?
  • On Nov 1, 2007, at 1:29 PM, Paul Bruneau wrote:

    > Not looking for an in-depth explanation (I should be able to find/
    > understand the docs once I get tipped in the right direction), but
    > can someone briefly hint to me the reason Apple uses key-value
    > coding in this example code:
    > http://developer.apple.com/documentation/Cocoa/Conceptual/NSPersistentDocum
    entTutorial/03_CustomClass/chapter_4_section_3.html

    >
    Because, for brevity, the tutorial didn't show the implementation of
    the custom accessors.

    For current best practice, see <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdAccessorMethods.html
    >.

    Feel free to file an enhancement request to ensure this is covered in
    an update to 10.5 for that tutorial.

    mmalc
  • On Nov 1, 2007, at 5:04 PM, mmalc crawford wrote:

    > On Nov 1, 2007, at 1:29 PM, Paul Bruneau wrote:
    >
    >> Not looking for an in-depth explanation (I should be able to find/
    >> understand the docs once I get tipped in the right direction), but
    >> can someone briefly hint to me the reason Apple uses key-value
    >> coding in this example code:
    >> http://developer.apple.com/documentation/Cocoa/Conceptual/
    >> NSPersistentDocumentTutorial/03_CustomClass/chapter_4_section_3.html
    >>
    > Because, for brevity, the tutorial didn't show the implementation
    > of the custom accessors.
    >
    > For current best practice, see <http://developer.apple.com/
    > documentation/Cocoa/Conceptual/CoreData/Articles/
    > cdAccessorMethods.html>.
    >
    > Feel free to file an enhancement request to ensure this is covered
    > in an update to 10.5 for that tutorial.

    I thank you for your reply, but I think your reply was aimed at a
    higher level than I meant to ask it.

    Please let me add some information. I am developing in and for 10.4
    at this time. I am just using basic Cocoa with Bindings. I am not
    using Core Data or Managed Objects (I don't think!).

    So my question is, do I have any reason to use valueForKey (like the
    example I referred to uses) or am I OK using the simple primitive
    accessors?

    I am interested in doing things Rightâ„¢ like being KVC/KVO compliant,
    but it's just that I am not sure what would spur me to use -
    valueForKey: rather than just [myObject myInstanceVariable]

    OK, I just re-found http://developer.apple.com/documentation/Cocoa/
    Conceptual/ModelObjects/Articles/moAccessorMethods.html#//apple_ref/
    doc/uid/TP40002132-SW1

    It seems to answer my question that unless I am using Core Data, I
    can stick with the simple accessor methods. Maybe someone could just
    verify that if it's true. For example, maybe there are reasons for me
    to go ahead and use things like -valueForKey even though today I am
    not using Core Data. This is where my ignorance lies.

    Thank you
  • > Hi-
    >
    > Not looking for an in-depth explanation (I should be able to find/
    > understand the docs once I get tipped in the right direction), but
    > can someone briefly hint to me the reason Apple uses key-value coding
    > in this example code:
    >
    > - (NSString *)fullNameAndID
    > {
    > return [NSString stringWithFormat:@"%@, %@ (%@)",
    > [self valueForKey:@"lastName"],
    > [self valueForKey:@"firstName"],
    > [self valueForKey:@"employeeID"]];
    > }
    >
    >
    > the example comes from
    > http://developer.apple.com/documentation/Cocoa/Conceptual/
    > NSPersistentDocumentTutorial/03_CustomClass/chapter_4_section_3.html
    >
    > Is there any advantage over doing it how I would tend to think to do
    > it?:
    >
    > - (NSString *)fullNameAndID
    > {
    > return [NSString stringWithFormat:@"%@, %@ (%@)",
    > [self lastName],
    > [self firstName],
    > [self employeeID]];
    > }

    KVC and accessor methods solve different problems.  KVC is providing a
    general purpose data access API by name.  It's more interesting when
    those strings are not constant strings.

    Usually, KVC actually works via the accessor methods.

    In this example, it's probably the case that this is Tiger based Core
    Data example code, and the author did not want to declare a custom
    subclass that implemented those accessor methods.  Or to most clearly
    answer your question, the author of this code felt like it for
    convenience.

    On Leopard, you can write the same code with the accessor method
    style, and Core Data will dynamically generate the accessors if you
    didn't implement them in a subclass.  Or rephrased, you can always
    invoke an accessor for a modeled property on an NSManagedObject, even
    if it's class is declared as 'NSManagedObject'

    > It must be because using -valueForKey: triggers something somewhere
    > but I don't quite have a handle on what. Is it better Cocoa practice
    > to just always use valueForKey? Or do I have to keep track in my head
    > on an object by object (or even accessor by accessor) basis when it
    > should be used?

    Uhm, the compiler will do that for you.  I prefer the accessor
    methods.  They are faster, and type/spell checked.  But KVC works with
    generic model objects like NSMutableDictionary and the super class
    NSManagedObject regardless of whether accessor methods even exist.
    Some people prefer KVC and a more generic/dynamic coding style.

    You really only *have* to use KVC if you find yourself trying to take
    a string and look up the method name and invoke it.  KVC is a lot
    easier for that than groping through runtime functions.

    - Ben
previous month november 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    
Go to today