谓词(NSPredicate)

OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便

下面来看一下具体的例子吧:

Person.h

 1 #import <Foundation/Foundation.h>  
 2   
 3 @interface Person : NSObject  
 4   
 5 @property NSString *name;  
 6 @property NSInteger age;  
 7   
 8 + (id)personWithName:(NSString *)name andAge:(NSInteger)age;  
 9   
10 @end  

Person.m

 1 #import "Person.h"  
 2   
 3 @implementation Person  
 4   
 5 + (id)personWithName:(NSString *)name andAge:(NSInteger)age{  
 6     Person *person = [[Person alloc] init];  
 7     person.name = name;  
 8     person.age = age;  
 9     return person;  
10 }  
11   
12 - (NSString *)description{  
13     NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];  
14     return s;  
15 }  
16   
17 @end 

测试方法

 

 1         NSArray *persons = [NSArray arrayWithObjects:  
 2                             [Person personWithName:@"mac" andAge:20],  
 3                             [Person personWithName:@"1" andAge:30],  
 4                             [Person personWithName:@"2" andAge:40],  
 5                             [Person personWithName:@"3" andAge:50],  
 6                             [Person personWithName:@"4" andAge:60],  
 7                             [Person personWithName:@"5" andAge:70],  
 8                             [Person personWithName:@"6" andAge:20],  
 9                             [Person personWithName:@"7" andAge:40],  
10                             [Person personWithName:@"8" andAge:60],  
11                             [Person personWithName:@"9" andAge:40],  
12                             [Person personWithName:@"0" andAge:80],  
13                             [Person personWithName:@"10" andAge:90],  
14                             [Person personWithName:@"1" andAge:20]];  
15           
16         //年龄小于30  
17         //定义谓词对象,谓词对象中包含了过滤条件  
18         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
19         //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
20         NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
21         NSLog(@"filterArray=%@",array);  
22           
23         //查询name=1的并且age大于40  
24         predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
25         array = [persons filteredArrayUsingPredicate:predicate];  
26         NSLog(@"filterArray=%@",array);  
27           
28         //in(包含)  
29         predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];  
30           
31         //name以a开头的  
32         predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];  
33         //name以ba结尾的  
34         predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];  
35           
36         //name中包含字符a的  
37         predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];  
38           
39         //like 匹配任意多个字符  
40         //name中只要有s字符就满足条件  
41         predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
42         //?代表一个字符,下面的查询条件是:name中第二个字符是s的  
43         predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];  

 

posted on 2017-02-16 14:30  Hauler  阅读(212)  评论(0编辑  收藏  举报

导航