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.

Filtering and matching with predicates<br />

219<br />

In some cases, this is less than desirable. It’s possible to override how nil values (typically<br />

used to represent the absence of a value) are handled. In the example of<br />

CTRentalProperty’s rentalPrice property, a more sensible approach may be to set<br />

the rentalPrice value to 0 whenever nil is received. You can do so by overriding<br />

another message called setNilValueForKey:, as demonstrated here:<br />

- (void)setNilValueForKey:(NSString *)key {<br />

if ([key isEqualToString:@"rentalPrice"]) {<br />

rentalPrice = 0;<br />

} else {<br />

[super setNilValueForKey:key];<br />

}<br />

}<br />

With this implementation of setNilValueForKey:, attempting to use KVC to set the<br />

rentalPrice property to nil instead causes the property to be updated to the value 0.<br />

11.3 Filtering and matching with predicates<br />

Now that you know how to reference and access object properties via string-based<br />

key paths, it may not surprise you that a layer of other technologies is built on top of<br />

this foundation.<br />

Often, when provided with an object, you’ll want to compare it against a certain<br />

condition and perform different actions based on the result of the comparison. Similarly,<br />

if you have a collection of data, such as an NSArray or NSSet, you may want to use<br />

a specific set of criteria to filter, or select, subsets of the data.<br />

These forms of expressions are commonly called predicate conditions and are represented<br />

in the Foundation framework by the NSPredicate class, which internally uses<br />

KVC, and key paths in particular.<br />

11.3.1 Evaluating a predicate<br />

Suppose you want to quickly determine if a CTRentalProperty object’s rental price<br />

is greater than $500. Mathematically, this can be expressed by the following predicate<br />

expression:<br />

rentalPrice > 500<br />

You can programmatically perform this check with an <strong>Objective</strong>-C statement such as<br />

the following:<br />

BOOL conditionMet = (house.rentalPrice > 500);<br />

The condition is hardcoded into the source code and can’t easily change during runtime<br />

or in response to user input. In such a case, the NSPredicate class decouples<br />

things in a manner similar to what KVC does for getter and setter operations. The<br />

example condition could also be evaluated by the following code snippet, which uses<br />

the NSPredicate class:<br />

NSPredicate *predicate =<br />

[NSPredicate predicateWithFormat:@"rentalPrice > 500"];<br />

BOOL conditionMet = [predicate evaluateWithObject:house];

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

Saved successfully!

Ooh no, something went wrong!