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.

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

11.1.4 Aggregating and collating values<br />

While working with one-to-many relationships in key paths, you may sometimes prefer an<br />

aggregate result that summarizes the raw data instead of an individual result for each<br />

value a key path matches. For example, rather than a list of tenants, you may just need the<br />

age of the oldest tenant, the average age of all tenants, or a list of all unique last names.<br />

Key-value paths can perform such calculations by introducing a collection operator<br />

into the key path using the @ prefix. In practice, most key paths consist of two<br />

parts; see table 11.1.<br />

Table 11.1<br />

Common key path collection operators that aggregate and summarize items in a collection<br />

Operator<br />

Description<br />

@avg<br />

@count<br />

@max<br />

@min<br />

@sum<br />

@distinctUnionOfObjects<br />

Converts the key path to the right of the operator to a collection<br />

of doubles and returns the average value<br />

Returns the number of objects in the key path to the left of<br />

the operator<br />

Converts the key path to the right of the operator to a collection<br />

of doubles and returns the maximum value<br />

Converts the key path to the right of the operator to a collection<br />

of doubles and returns the minimum value<br />

Converts the key path to the right of the operator to a collection<br />

of doubles and returns their sum<br />

Returns an array containing the distinct objects in the property<br />

specified by the key path to the right of the operator<br />

As an example, the following code snippet uses the @count operator to determine the<br />

number of rental properties located in the rentalProperties array:<br />

NSNumber *count = [rentalProperties valueForKeyPath:"@count"];<br />

NSLog(@"There are %d rental properties for available", [count intValue]);<br />

Internally, KVC queries a key path as normal, but when it detects an aggregate function,<br />

it creates a loop that iterates over all matching elements and performs the<br />

required calculation. The neat thing is that this process is all hidden from you: you<br />

express in a string the result you need. For example, the following two queries return<br />

the maximum and average length of all rental property addresses:<br />

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

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

Another thing you might need is a set of unique values assigned to a given property,<br />

such as a list of all unique rental prices. You may be tempted to generate the list via<br />

the KVC query:<br />

NSArray *rentalPrices = [rentalProperties valueForKeyPath:@"rentalPrice"];

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

Saved successfully!

Ooh no, something went wrong!