官方例子
1 以a开头的
NSMutableArray*array =
[NSMutableArrayarrayWithObjects:@"Nick",@"Ben",@"Adam",@"Melissa",nil];
NSPredicate*bPredicate = [NSPredicatepredicateWithFormat:@"SELF beginswith[c] 'a'"];
NSArray*beginWithB = [array filteredArrayUsingPredicate:bPredicate];
// beginWithB contains { @"Adam" }.
2 包含字母e的
NSPredicate*sPredicate = [NSPredicatepredicateWithFormat:@"SELF contains[c] 'e'"];
[arrayfilterUsingPredicate:sPredicate];
// array now contains { @"Ben", @"Melissa" }
3firstName和Ben 类似的
NSString*firstName =@"Ben";
NSArray*array =@[@{@"lastName":@"Turner"},
@{@"firstName":@"Ben",@"lastName":@"Ballard",
@"birthday":@"1972-03-24 10:45:32"}];
NSPredicate*predicate =
[NSPredicatepredicateWithFormat:@"firstName like %@", firstName];
NSArray*filteredArray = [arrayfilteredArrayUsingPredicate:predicate];
NSLog(@"filteredArray: %@", filteredArray);
4firstName存在或者为空的
NSString*firstName =@"Ben";
NSArray*array =@[@{@"lastName":@"Turner"},
@{@"firstName":@"Ben",@"lastName":@"Ballard",
@"birthday":@"1972-03-24 10:45:32"}];
NSPredicate*predicate = [NSPredicatepredicateWithFormat:@"(firstName == %@) || (firstName = nil)", firstName];
NSArray*filteredArray = [arrayfilteredArrayUsingPredicate:predicate];
NSLog(@"filteredArray: %@", filteredArray);
网友评论