美文网首页
NSPredicate官方例子

NSPredicate官方例子

作者: FengxinLi | 来源:发表于2016-01-08 17:13 被阅读60次

官方例子

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);

相关文章

网友评论

      本文标题:NSPredicate官方例子

      本文链接:https://www.haomeiwen.com/subject/xxafkttx.html