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 />

223<br />

Table 11.4 Set-based operators that can be used in NSPredicate-based expressions when a<br />

collection of objects is present in a key path<br />

Operator Description Example<br />

ANY<br />

Do one or more objects satisfy the key path<br />

expression?<br />

ANY address.length > 50<br />

ALL Do all objects satisfy the key path expression? ALL address.length < 20<br />

NONE<br />

Do all objects not match the key path<br />

expression?<br />

NONE address.length < 20<br />

11.3.6 Parameterizing and templating predicate expressions<br />

So far, all examples of creating an NSPredicate object have used the predicateWith-<br />

Format: class message and consisted of a simple string. As the name of the message<br />

alludes to, the predicateWithFormat: message accepts NSString stringWith-<br />

Format:–style format arguments as well. For example, the following code snippet produces<br />

the predicate expression rentalPrice > 500 || address ENDSWITH 'Sumner':<br />

NSPredicate *predicate = [NSPredicate predicateWithFormat:<br />

@"rentalPrice > %f || address ENDSWITH %@", 500.0f, @"Sumner"];<br />

Notice that, unlike the original example in which the string Sumner had to be escaped<br />

with quotation marks, this example uses the %@ format specifier, and NSPredicate is<br />

smart enough to introduce the required quote marks. The following code sample<br />

emits the final predicate expression generated by the previous code snippet:<br />

NSLog(@"The predicate expression is %@", [predicate predicateFormat]);<br />

This feature and others like it can be handy, and they help avoid common problems in<br />

generating dynamic expressions, such as improperly escaping strings that contain<br />

internal quotation marks.<br />

If you parameterize a predicate expression because you want to dynamically<br />

adjust its values, perhaps by hooking up a numeric value to a UISlider control, the<br />

NSPredicate class can go one better. Altering the previous code snippet, you can use<br />

the following predicate template definition<br />

NSPredicate *template = [NSPredicate predicateWithFormat:<br />

@"rentalPrice > $MINPRICE || address ENDSWITH $SUBURB"];<br />

where names beginning with a dollar sign indicate placeholders in the templates<br />

where you can substitute different values at runtime. If you attempt to use this template<br />

predicate directly, an NSInvalidArgumentException is thrown, indicating that<br />

you haven’t provided values for MINPRICE and SUBURB. To specify these values, you use<br />

the predicateWithSubstitutionVariables: message, passing in an NSDictionary of<br />

variable name/value pairs, as follows:

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

Saved successfully!

Ooh no, something went wrong!