!!!Obj-c on Mac --- Chapter 17 NSPredicate
2014-01-03 10:24
661 查看
You can create NSPredicate objects that describe exactly what you think is the truth and run each of your objects through the predicate to see if they match.
If a chunk of text in the predicate string is not quoted, it is treated as a key path. If it’s quoted, it’s treated as a literal string. You can use single quotes or double quotes (as long as they’re balanced). Usually, you’ll use single quotes; otherwise,
you’ll have to escape each double quote in the string.
to “Herbie”. If the name and “Herbie” are the same, -evaluateWithObject: returns YES, otherwise NO.
You can put in numerical values with %d and %f like you’re familiar with:
Creating a Predicate
One way to create predicate object is using query strings.NSPredicate *predicate; predicate = [NSPredicate predicateWithFormat: @"name == 'Herbie'"];+predicateWithFormat: takes the provided string and builds a tree of objects behind the scenes that will be used to evaluate the predicate.
If a chunk of text in the predicate string is not quoted, it is treated as a key path. If it’s quoted, it’s treated as a literal string. You can use single quotes or double quotes (as long as they’re balanced). Usually, you’ll use single quotes; otherwise,
you’ll have to escape each double quote in the string.
Evaluate the Predicate
BOOL match = [predicate evaluateWithObject: car]; NSLog (@"%s", (match) ? "YES" : "NO");-evaluateWithObject: tells the receiving object (the predicate) to evaluate itself with the given object. In this case, it takes the car, applies valueForKeyPath: using name as the key path to get the name. Then, it compares it for equality
to “Herbie”. If the name and “Herbie” are the same, -evaluateWithObject: returns YES, otherwise NO.
NSArray *cars = [garage cars]; for (Car *car in [garage cars]) { if ([predicate evaluateWithObject: car]) { NSLog (@"%@", car.name); } }-filteredArrayUsingPredicate: is a category method on NSArray that will spin through the contents of the array, evaluate each object against a predicate, and accumulate objects that evaluate to YES into a new array that is returned:
NSArray *results; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@", results);
Format Specifiers
We can put varying stuff into our predicate format strings in two ways: format specifiers and variable names.You can put in numerical values with %d and %f like you’re familiar with:
predicate = [NSPredicate predicateWithFormat: @"name == %@", @"Herbie"];NSPredicate strings also let you use %K to specify a key path. This predicate is the same as the others, using name == 'Herbie' as the truth:
predicate = [NSPredicate predicateWithFormat: @"%K == %@", @"name", @"Herbie"];Using format specifiers is one way to have flexible predicates. The other involves putting variable names into the string, similar to environment variables:
predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower > $POWER"]; varDict = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: 150], @"POWER", nil]; predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];
Operator
The syntax also supports parenthetical expressions (really!) and the AND, OR, and NOT logical operators or their C-looking equivalents &&, ||, and !.predicate = [NSPredicate predicateWithFormat: @"(engine.horsepower > 50) AND (engine.horsepower < 200)"];
相关文章推荐
- !!!Obj-c on Mac --- Chapter 8 Foundation Kit
- !!!Obj-c on Mac --- Chapter 13 Protocols
- !!!Obj-c on Mac --- Chapter 16 Key-Value Coding
- !!!Obj-c on Mac --- Chapter 2 ~ 4
- !!!Obj-c on Mac --- Chapter 9 Memory Management
- !!!Obj-c on Mac --- Chapter 5 ~ 7
- !!!Obj-c on Mac --- Chapter 10 - 12 Initialization Property Category
- Custom Views(Chapter 17 of Cocoa Programming for Mac OS X)
- !!!Obj-C 2.0 -- Chapter 3 Allocating and Initializing Objects
- Basic Core Data(Chapter 11 of Cocoa Programming for Mac OS X)
- Localization(Chapter 16 of Cocoa Programming for Mac OS X)
- View Swapping(Chapter 29 of Cocoa Programming for Mac OS X)
- Chapter 3 Phenomenon——17
- Think Python - Chapter 17 - Classes and methods
- [Career Cup 10][subjects from 52~56] Chapter 17 Network
- Chapter 17 Replication 复制
- Chapter 1 First Sight——17
- Copying Objects(Chapter 17 of Programming in Objective-C 2.0)
- Foundation Kit「Learn Objective-C on Mac
- 《C++捷径教程》读书笔记--Chapter 17--异常处理(完结)