• 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: When to use key-value coding to get values vs. accessors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: re: When to use key-value coding to get values vs. accessors
  • From: Ben Trumbull <email@hidden>
  • Date: Sat, 3 Nov 2007 16:47:45 -0700

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


_______________________________________________

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


  • Prev by Date: [Moderator] Please take this to another forum - Re: Adding an icon to the dock
  • Next by Date: Re: Why initialize the menubar without Interface Builder
  • Previous by thread: Re: When to use key-value coding to get values vs. accessors
  • Next by thread: NSAmimatablePropertyContainer property animation troubles...
  • Index(es):
    • Date
    • Thread