01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

214 CHAPTER 11 Key-Value Coding and NSPredicate<br />

For the matching setValue:forKey: message to work correctly, the object must provide<br />

one of the following messages:<br />

■<br />

■<br />

■<br />

A setter method named setKey:<br />

An instance variable named _key or _isKey<br />

An instance variable named key or isKey<br />

By conforming to these conventions, you enable the KVC framework to use <strong>Objective</strong>-C<br />

class metadata to determine how to interact with your objects at runtime. Although<br />

the framework supports instance variables beginning with an underscore (_) prefix,<br />

they should be avoided because the underscore prefix is typically reserved for internal<br />

use by Apple and the <strong>Objective</strong>-C runtime.<br />

Most objects are KVC-compliant by default<br />

It’s important to realize that almost all <strong>Objective</strong>-C objects, including those in Foundation<br />

framework, such as NSString, are by default suitable for use with KVC techniques.<br />

This is because every object ultimately inherits from NSObject, and the<br />

@synthesize compiler feature discussed in chapter 5 produces suitable getter and<br />

setter methods automatically by default. In general, you have to go out of your way<br />

to produce an object that’s incompatible with KVC.<br />

11.1.1 Accessing properties via KVC<br />

Now that you know how to make objects compatible with KVC, let’s look at how the<br />

KVC mechanism can be used to get and update the current value of properties.<br />

To query an object for the current value of one of its properties, you can use the<br />

valueForKey: message. A similar setValue:forKey: message allows you to specify a<br />

new value for a property:<br />

NSLog(@"House address is: %@", [house valueForKey:@"address"]);<br />

[house setValue:@"42 Campbell Street" forKey:@"address"];<br />

Notice the prototype of the valueForKey: method indicates that it accepts an object<br />

(id). This means you can’t pass in values for primitive data types such as int, float, or<br />

BOOL directly. You must box these via an instance of the NSNumber class. The KVC<br />

framework automatically unboxes these values as required, as shown here:<br />

NSNumber *price = [house valueForKey:@"rentalPrice"];<br />

NSLog(@"House rental price is $%f ", [price floatValue]);<br />

[house setValue:[NSNumber numberWithFloat:19.25] forKey:@"rentalPrice"];<br />

As a convenience measure, KVC also provides the dictionaryWithValuesForKeys:<br />

and setValuesForKeysWithDictionary: methods that enable you to pass in multiple<br />

KVC requests at once. In the case of the dictionaryWithValuesForKeys: message,<br />

you pass in an NSArray of property names and get back an NSDictionary of property<br />

name/value pairs.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!