关闭

NSPredicate详解一

422人阅读 评论(0) 收藏 举报
分类:

NSPredicate是Foundation框架提供的用来指定过滤条件的类。可以通过一种类似SQL where 从句的谓语查询语言创建谓语对象。主要运用在集合中筛选子项和数据库查询,也可以用于字符串的正则匹配。

常用的谓语对象初始化方法:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name=%@",@"hark"];

谓语需要对集合中的对象值进行比较,在此例中集合的每一个对象都必须有name这个属性

NSArray * listObjects = @[@{@"name":@"james",@"age":@(32)},@{@"name":@"peter", @"age":@(29)}, @{@"name":@"hark", @"age":@(28)}];

一个简单的例子:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name=%@",@"hark"];
NSArray * result = [listObjects filteredArrayUsingPredicate:predicate];
NSLog(@"%@", result);		//output:({name=hark})

谓语支持的比较操作

基本比较操作

==(=),>=(=>),<=(=<),>,<,!=(<>),BETWEEN

前面的都简单,没什么好说的,BETWEEN的使用

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"age BETWEEN {20, 30}"];
NSArray * result = [listObjects filteredArrayUsingPredicate:predicate]
NSLog(@"%@", result); 		//output:({name=peter;age=29},{name=hark;age=28})

字符串比较操作

  • BEGINSWITH (左值以右值开头)

NSPredicate * beginswithPredicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'h'"];
result =[listObjects filteredArrayUsingPredicate:beginswithPredicate];
NSLog(@"%@", result);		//output:({age=28;name=hark})

  • CONTAINS (左值包含右值)

NSPredicate * containsPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'et'"];
result =[listObjects filteredArrayUsingPredicate:beginswithPredicate];
NSLog(@"%@", result);		//output:({age=29;name=peter})

  • ENDSWITH (左值以右值结尾)
NSPredicate * endswithPredicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 's'"];
result = [listObjects filteredArrayUsingPredicate:endswithPredicate];
NSLog(@"%@", result);		//output:({age=32;name=james})

  • LIKE (左值等于右值,可使用?和*通配符,?匹配单个字符,*匹配0~n个字符)
NSPredicate * likePredicate = [NSPredicate predicateWithFormat:@"name LIKE *et*"];
result = [listObjects filteredArrayUsingPredicate:likePredicate];
NSLog(@"%@", result);		//output:({age=29;name=peter})

  • MATCHES (左值使用右侧正则进行匹配)
NSPredicate * matchesPredicate = [NSPredicate predicateWithFormat:@"name MATCHES '^[ph].*'"];
result = [listObjects filteredArrayUsingPredicate:likePredicate];
NSLog(@"%@", result);		//output:({age=29;name=peter},{age=28;name=hark})

字符串比较选项

[c] 大小写不敏感

[d] 忽略发音符

[n] 表明操作对象已经预处理(大小写已转换,替换发音符等等...),设置该项时会忽略已设置的[c][d]项

[l] 表明需要考虑本地字符集


NSPredicate *optionPredicate = [NSPredicate predicateWithFormat:@"name = [d]'hàrk'"];
result = [listObjects filteredArrayUsingPredicate:optionPredicate];
NSLog(@"%@", result);       //output:({age=28;name=hark})

集合操作

  • ANY,SOME,ALL,NONE (对集合进行匹配,返回的结果也是集合)
NSArray * wrapListObjects = @[listObjects];
NSPredicate * anyPredicate = [NSPredicate predicateWithFormat:@"ANY SELF.age > 30"];
result = [wrapListObjects filteredArrayUsingPredicate:anyPredicate];
NSLog(@"%@", result);	//output:(({age=32;name=james},{age=29;name=peter},{age=32;name=}))
NSPredicate * allPredicate = [NSPredicate predicateWithFormat:@"ALL SELF.age > 30"];
result = [wrapListObjects filteredArrayUsingPredicate:allPredicate];
NSLog(@"%@", result);	//output:(())

  • IN
NSPredicate * inPredicate = [NSPredicate predicateWithFormat:@"name in {'hark', 'paul', 'jimmy'}"];
result = [listObjects filteredArrayUsingPredicate:inPredicate];
NSLog(@"%@", result);	//output:({age=28,name=hark})

逻辑运算操作

AND(&&),OR(||),NOT(!)

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@ AND age BETWEEN {%d, %d}", @"h", 20, 30];


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4923次
    • 积分:114
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档