01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Making your objects KVC-compliant<br />

215<br />

11.1.2 Constructing key paths<br />

KVC is much more interesting once you discover that as well as using simple keys that<br />

access properties directly on the object being queried, it’s possible to use more<br />

advanced key paths to drill down into child objects. To enable this functionality, you<br />

use the valueForKeyPath: message instead of valueForKey:.<br />

In the key path string, you separate the name of properties with periods, and as the<br />

KVC framework parses the string, it steps through your object hierarchy using KVC<br />

queries to reach the next level. As an example, the CTRentalProperty class has an<br />

address property that returns an NSString, and an NSString object has a length<br />

property. You can query the character length of a property’s address via the following<br />

KVC key path query:<br />

NSNumber *len = [house valueForKeyPath:@"address.length"];<br />

NSLog(@"The address has %d characters in it", [len intValue]);<br />

There is also an equivalent setValue:forKeyPath: that can be used to update the<br />

current value of a property of a child object, provided it has a suitable setter message.<br />

11.1.3 Returning multiple values<br />

While traversing key paths, you may encounter a property that represents zero or more<br />

values. For example, within chapter 6 you created a Teacher class which contained an<br />

NSArray property listing the classes taught by that teacher.<br />

An array property represents a one-to-many relationship. Once an array is found in<br />

a key path, the remaining part is evaluated for each element in the array, and the<br />

results collated into an array.<br />

As an example, the following query uses valueForKeyPath: to determine the<br />

classes taught by the list of teachers found within an array called myTeachers:<br />

NSArray * courses = [myTeachers valueForKeyPath:@"classes"];<br />

NSLog(@"Courses taught by teachers: %@", courses);<br />

This query generates output similar to this:<br />

Courses taught by teachers: (English, Spanish, Math, ARM, Immersive Gaming,<br />

Physical Computing)<br />

KVC can use collections other than NSArrays<br />

Although the examples in this chapter heavily rely on properties that return an<br />

NSArray collection, the KVC infrastructure can operate with other collection-based<br />

data structures, even custom ones you develop yourself.<br />

The key, yet again, is ensuring your class provides support for messages that conform<br />

to a specific convention to enable access to the collection. For more information,<br />

refer to the section titled “Collection Accessor Patterns for To-Many Properties”<br />

in the Key-Value Coding Programming Guide.

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

Saved successfully!

Ooh no, something went wrong!