NSPredicate 用于指定过滤条件,主要用于从集合中分拣出符合条件的对象,也可以用于字符串的正则匹配。
NSPredicate常用方法介绍
[objc] view plaincopy
- 1.创建NSPredicate(相当于创建一个过滤条件)
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"过滤条件"];
- 2.判断指定的对象是否满足NSPredicate创建的过滤条件
- [predicate evaluateWithObject:person];
- 3.过滤出符合条件的对象(返回所有符合条件的对象)
- NSArray *persons = [array filteredArrayUsingPredicate:predicate];
实例:(以下为伪代码,只为讲解问题)
[objc] view plaincopy
- 1.先创建一个person对象
- @interface Person: NSObject{
- NSString *name;
- int age;
- }
- 2.创建一个数组,在数组种加入多个person对象
- NSArray *array=[NSArray arrayWithObjects:person1,person2,person3,person4,...,nil];
- 3.使用NSPredicate来过滤array种的person
- a.找出array种age小于20的person
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age < 20"];
- for(Person *person in array){
- if([predicate evaluateWithObject:person]){ //判断指定的对象是否满足
- //........................
- }
- }
- NSArray *persons = [array filteredArrayUsingPredicate:predicate];//获取所有age小于20的person
使用方法主要就这几步,以下讲一些常用的NSpredicate的条件
1.逻辑运算符号 > , < , = , >= , <= 都能使用在这里
运算符还可以跟逻辑运算符一起使用,&& , || ,AND, OR 谓词不区分大小写
[objc] view plaincopy
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age > 20"];
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name > ‘abc‘ && age > 10"];
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name > ‘abc‘ OR age > 10"];
[objc] view plaincopy
- <span style="font-family: Arial, Helvetica, sans-serif;"></span><p class="p1">
- </p>
2.IN
[objc] view plaincopy
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN {‘abc‘ , ‘def‘ , ‘123‘}"];
3.以xx开头 --beginswith
[objc] view plaincopy
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH ‘N‘"];//name以N打头的person
[objc] view plaincopy
- </pre>4.以xx结尾--endswith</div><div></div><div><pre code_snippet_id="163702" snippet_file_name="blog_20140120_8_9862346" name="code" class="objc">NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH ‘N‘"];//name以N结尾的person
5.包含 -- contains
[objc] view plaincopy
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS ‘N‘"]; <span style="font-family: Arial, Helvetica, sans-serif;">//name种包含‘N的person</span>
6.模糊查询--like
[objc] view plaincopy
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE ‘*N*‘"];//<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14.44444465637207px; line-height: 26px;">*表示零个或多个字符</span>
7.以上说的都是对象种的属性匹配,如果数组种都是字符串如何匹配--self
[objc] view plaincopy
- NSArray *array=[NSArray arrayWithObjects: @"abc", @"def", @"ghi",@"jkl", nil nil];
- NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF==‘abc‘"];
- NSArray *array2 = [array filteredArrayUsingPredicate:pre];
8.正则表达式
[objc] view plaincopy
- (8.) 正则表达式:
- NSPredicate 使用MATCHES 匹配正则表达式,正则表达式的写法采用international components
- for Unicode (ICU)的正则语法。
- 例:
- NSString *regex = @"^A.+e$";//以A 开头,以e 结尾的字符。
- NSPredicate *pre= [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
- if([pre evaluateWithObject: @"Apple"]){
- printf("YES\n");
- }else{
- printf("NO\n");
- }
备注:以上为本人浅见,如有不对之处,请各位大大们指出纠正,谢谢!!
时间: 2024-10-26 11:13:54