I have moved my blog to Wordpress at theunixgeek.wordpress.com. I will still be checking back periodically on this one as well, though. 19 April 2009

featured

Merging Mkdir and Cd | 280 Slides Interview | I Switched to KDE 4

clickable portals

Friday, September 26, 2008

An Introduction to Key-Value Coding in Cocoa

Key-value coding is a powerful technology available in Cocoa, but it may be difficult for beginners to understand. But without key-value coding, you couldn't easily make applications scriptable or use bindings in your applications. Also, using key-value coding can significantly reduce how much code you write by eliminating accessor methods. Using it may even increase your code's security by not needing to access instance variables directly. I believe that's enough to be convinced to use this technology, but how should one go about doing so?

First off, we need to learn some vocabulary:

  • property - an object value (specifically attributes, to-one relationships, and to-many relationships) that can be accessed by KVC.
  • attribute - a property that is represented as a simple value. Examples include immutable (non-editable) objects, numbers, strings, booleans, etc.
  • to-one relationship - a property in which an object has properties of its own
  • to-many relationship - a property that consists of a collection of related objects, such as an NSArray
  • key - a string that identifies a specific property of an object. 
In case you don't know how to declare properties in an Objective-C class, here's a sample of a basic AppleFruit class:

@interface AppleFruit : NSObject
{
  NSNumber *price;
}
@property(copy, readwrite) NSNumber *price;
@end

@implementation AppleFruit
@synthesize price;
@end;


Of course, you could get a lot fancier with properties, but there's the bare basics of it.

Now, let's get further acquainted with keys. Keys typically correspond to the name of an accessor method or an instance variable in an object. Quoting the documentation,
Keys must use ASCII encoding, begin with a lowercase letter, and may not contain whitespace.
If you wanted to get the value of a key, you would simply use the valueForKey: method. For example:
[appleFruit setValue:23 forKey:price];
[priceTextField setStringValue:[appleFruit valueForKey:price]];


could be used in another class to set and get the price value of the appleFruit object, respectively.

Here's what the class could look like if key-value coding weren't implemented in it:

@interface AppleFruit : NSObject
{
   NSNumber *price;
}
- (NSNumber *)returnPrice;
- (void)setPrice:(NSNumber *)newPrice;
@end

@implementation AppleFruit

- (NSNumber *)returnPrice
{
   return price;
}

- (void)setPrice:(NSNumber *)newPrice
{
   price = newPrice;
}

@end;


See how much code was spared? Good luck with implementing KVC in your applications!

Note: this is a summarized beginner-friendly version of the first three sections of the Key-Value Coding Programming Guide available through Apple's documentation. Bits were also taken from the Design Guidelines and The Objective-C 2.0 Programming Language.

2 comments:

Nishant Varma said...

Hey there

Nishant Varma said...

whats ur email id ? i have a query regarding unix career . i have got into a IT company in a system admin role . however i want a more creative and productive career and more into programming area ... what language shud I learn ? python ? also do you think admins can switch from a admin career to programming career ? ...please sent me at nishant.varma at gmail dot com...

hope i am not eating your time there
thanks
nishant