谓词(NSPredicate)

谓词(NSPredicate)

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

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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  
我们在Person类中定义属性,还有一个产生对象的类方法,同时重写了description方法,用于打印结果


测试方法

main.m

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import <Foundation/Foundation.h>  
  2. #import "Person.h"  
  3.   
  4. //谓词,指定过滤器的条件,将符合条件的对象保留下来  
  5. //一般用谓词过滤数组中指定的元素  
  6. int main(int argc, const charchar * argv[]) {  
  7.     @autoreleasepool {  
  8.          
  9.         NSArray *persons = [NSArray arrayWithObjects:  
  10.                             [Person personWithName:@"mac" andAge:20],  
  11.                             [Person personWithName:@"1" andAge:30],  
  12.                             [Person personWithName:@"2" andAge:40],  
  13.                             [Person personWithName:@"3" andAge:50],  
  14.                             [Person personWithName:@"4" andAge:60],  
  15.                             [Person personWithName:@"5" andAge:70],  
  16.                             [Person personWithName:@"6" andAge:20],  
  17.                             [Person personWithName:@"7" andAge:40],  
  18.                             [Person personWithName:@"8" andAge:60],  
  19.                             [Person personWithName:@"9" andAge:40],  
  20.                             [Person personWithName:@"0" andAge:80],  
  21.                             [Person personWithName:@"10" andAge:90],  
  22.                             [Person personWithName:@"1" andAge:20]];  
  23.           
  24.         //年龄小于30  
  25.         //定义谓词对象,谓词对象中包含了过滤条件  
  26.         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
  27.         //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
  28.         NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
  29.         NSLog(@"filterArray=%@",array);  
  30.           
  31.         //查询name=1的并且age大于40  
  32.         predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
  33.         array = [persons filteredArrayUsingPredicate:predicate];  
  34.         NSLog(@"filterArray=%@",array);  
  35.           
  36.         //in(包含)  
  37.         predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];  
  38.           
  39.         //name以a开头的  
  40.         predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];  
  41.         //name以ba结尾的  
  42.         predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];  
  43.           
  44.         //name中包含字符a的  
  45.         predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];  
  46.           
  47.         //like 匹配任意多个字符  
  48.         //name中只要有s字符就满足条件  
  49.         predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
  50.         //?代表一个字符,下面的查询条件是:name中第二个字符是s的  
  51.         predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];  
  52.           
  53.           
  54.           
  55.     }  
  56.     return 0;  
  57. }  
首先我们看到,我们初始化了一定大小的数组。

然后我们就可以使用NSPredicate类进行过滤操作了


1、查询数组中年龄小于30的对象

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //年龄小于30  
  2. //定义谓词对象,谓词对象中包含了过滤条件  
  3. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
  4. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
  5. NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
  6. NSLog(@"filterArray=%@",array);  
首先创立一个过滤条件:

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //定义谓词对象,谓词对象中包含了过滤条件  
  2. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
这里面操作很简单的:@"age<%d",这个age是Person的属性名,%d相当于占位符,然后后面用参数替换即可

然后进行过滤操作,返回一个过滤之后的数组对象

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
  2. NSArray *array = [persons filteredArrayUsingPredicate:predicate];  


2、查询name=1并且age大于40的集合

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //查询name=1的并且age大于40  
  2. predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
  3. array = [persons filteredArrayUsingPredicate:predicate];  
  4. NSLog(@"filterArray=%@",array);  
当然我们也可以使用&&进行多条件过滤


3、包含语句的使用

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //in(包含)  
  2. predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];  


4、指定字符开头和指定字符结尾,是否包含指定字符

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //name以a开头的  
  2. predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];  
  3. //name以ba结尾的  
  4. predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];  
  5.   
  6. //name中包含字符a的  
  7. predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];  


5、like进行匹配多个字符

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //like 匹配任意多个字符  
  2. //name中只要有s字符就满足条件  
  3. predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
  4. //?代表一个字符,下面的查询条件是:name中第二个字符是s的  
  5. predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值