NSPredicate类

NSPredicate,指定过滤器的条件

 1 // 设置谓词条件
 2 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age <= 28"];
 3 for (Person *person in array)
 4 {
 5     // 表示指定的对象是否满足谓词条件
 6     if ([predicate evaluateWithObject:person])
 7     {
 8         //NSLog(@"person name : %@", person.name);
 9     } 
10 }
11 
12 // 返回一个符合谓词条件的数组
13 NSArray *newArray = [array filteredArrayUsingPredicate:predicate];
14 
15 // 格式占位符号
16 NSPredicate *pre = [NSPredicate predicateWithFormat:@" age <= %d", 30];
17 NSArray *array2 = [array filteredArrayUsingPredicate:pre];
18 
19 
20 // 运算符号 的加入 谓词不区分大小 && AND || OR
21 NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"name > 'bruse' && age < %d", 30];
22 NSArray *array4 = [array filteredArrayUsingPredicate:pre3];
23 
24 
25 // 关键字 注意字符串⼀一定要添加''
26 NSPredicate *pre4 = [NSPredicate predicateWithFormat:@"self.name IN {'rose', 'bruse'}"];
27 NSArray *array5 = [array filteredArrayUsingPredicate:pre4];
28 
29 // BEGINSWITH 检查某个字是否以**开头
30 NSPredicate *pre5 = [NSPredicate predicateWithFormat:@"self.name BEGINSWITH 'J'"];
31 NSArray *array6 = [array filteredArrayUsingPredicate:pre5];
32 
33 // ENDSWITH 检查某个字符是以**结尾
34 NSPredicate *pre6 = [NSPredicate predicateWithFormat:@"self.name endswith 'e'"];
35 NSArray *array7 = [array filteredArrayUsingPredicate:pre6];
36 
37 // CONTAINS 检查包含某个字符
38 NSPredicate *pre8 = [NSPredicate predicateWithFormat:@"self.name CONTAINS '小'"];
39 NSArray *array8 = [array filteredArrayUsingPredicate:pre8];
40 
41 // Like 检查包含某个字符
42 NSString *s = [NSString stringWithFormat:@"name like '*%@*'",@"a"];
43 NSPredicate *predicate = [NSPredicate predicateWithFormat:s];
原文地址:https://www.cnblogs.com/cmembd/p/3770993.html