OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。
下面来看一下具体的例子吧:
Person.h
//
// Person.h
// 46_NSPredicate
//
// Created by jiangwei on 14-10-18.
// Copyright (c) 2014年 jiangwei. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property NSString *name;
@property NSInteger age;
+ (id)personWithName:(NSString *)name andAge:(NSInteger)age;
@end
Person.m
//
// Person.m
// 46_NSPredicate
//
// Created by jiangwei on 14-10-18.
// Copyright (c) 2014年 jiangwei. All rights reserved.
//
#import "Person.h"
@implementation Person
+ (id)personWithName:(NSString *)name andAge:(NSInteger)age{
Person *person = [[Person alloc] init];
person.name = name;
person.age = age;
return person;
}
- (NSString *)description{
NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];
return s;
}
@end
我们在Person类中定义属性,还有一个产生对象的类方法,同时重写了description方法,用于打印结果
测试方法
main.m
// // main.m // 46_NSPredicate // // Created by jiangwei on 14-10-18. // Copyright (c) 2014年 jiangwei. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" //谓词,指定过滤器的条件,将符合条件的对象保留下来 //一般用谓词过滤数组中指定的元素 int main(int argc, const char * argv[]) { @autoreleasepool { NSArray *persons = [NSArray arrayWithObjects: [Person personWithName:@"mac" andAge:20], [Person personWithName:@"1" andAge:30], [Person personWithName:@"2" andAge:40], [Person personWithName:@"3" andAge:50], [Person personWithName:@"4" andAge:60], [Person personWithName:@"5" andAge:70], [Person personWithName:@"6" andAge:20], [Person personWithName:@"7" andAge:40], [Person personWithName:@"8" andAge:60], [Person personWithName:@"9" andAge:40], [Person personWithName:@"0" andAge:80], [Person personWithName:@"10" andAge:90], [Person personWithName:@"1" andAge:20]]; //年龄小于30 //定义谓词对象,谓词对象中包含了过滤条件 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30]; //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果 NSArray *array = [persons filteredArrayUsingPredicate:predicate]; NSLog(@"filterArray=%@",array); //查询name=1的并且age大于40 predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"]; array = [persons filteredArrayUsingPredicate:predicate]; NSLog(@"filterArray=%@",array); //in(包含) predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"]; //name以a开头的 predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"]; //name以ba结尾的 predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"]; //name中包含字符a的 predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"]; //like 匹配任意多个字符 //name中只要有s字符就满足条件 predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"]; //?代表一个字符,下面的查询条件是:name中第二个字符是s的 predicate = [NSPredicate predicateWithFormat:@"name like '?s'"]; } return 0; }
首先我们看到,我们初始化了一定大小的数组。
然后我们就可以使用NSPredicate类进行过滤操作了
1、查询数组中年龄小于30的对象
//年龄小于30 //定义谓词对象,谓词对象中包含了过滤条件 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30]; //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果 NSArray *array = [persons filteredArrayUsingPredicate:predicate]; NSLog(@"filterArray=%@",array);
首先创立一个过滤条件:
//定义谓词对象,谓词对象中包含了过滤条件 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
这里面操作很简单的:@"age<%d",这个age是Person的属性名,%d相当于占位符,然后后面用参数替换即可
然后进行过滤操作,返回一个过滤之后的数组对象
//使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray *array = [persons filteredArrayUsingPredicate:predicate];
2、查询name=1并且age大于40的集合
//查询name=1的并且age大于40 predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"]; array = [persons filteredArrayUsingPredicate:predicate]; NSLog(@"filterArray=%@",array);
当然我们也可以使用&&进行多条件过滤
3、包含语句的使用
//in(包含) predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];4、指定字符开头和指定字符结尾,是否包含指定字符
//name以a开头的 predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"]; //name以ba结尾的 predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"]; //name中包含字符a的 predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];5、like进行匹配多个字符
//like 匹配任意多个字符 //name中只要有s字符就满足条件 predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"]; //?代表一个字符,下面的查询条件是:name中第二个字符是s的predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
like 例子:
NSArray *arrayContent = [NSArray arrayWithObjects:@"a1", @"abc11", @"abc14", @"abc2", nil];
NSString *match = @"abc1*";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];
NSArray *results = [arrayContent filteredArrayUsingPredicate:predicate];
引用最多的一篇:一般来说这种情况还是蛮多的,比如你从文件中读入了一个array1,然后想把程序中的一个array2中符合array1中内容的元素过滤出来。
正 常傻瓜一点就是两个for循环,一个一个进行比较,这样效率不高,而且代码也不好看。
其实一个循环或者无需循环就可以搞定了,那就需要用搞 NSPredicate这个类了~膜拜此类~
1)例子一,一个循环
NSArray *arrayFilter = [NSArray arrayWithObjects:@"pict", @"blackrain", @"ip", nil]; NSArray *arrayContents = [NSArray arrayWithObjects:@"I am a picture.", @"I am a guy", @"I am gagaga", @"ipad", @"iphone", nil]; |
int i = 0, count = [arrayFilter count]; for(i = 0; i < count; i ++) { NSString *arrayItem = (NSString *)[arrayFilter objectAtIndex:i]; NSPredicate *filterPredicate = [[NSPredicate predicateWithFormat:@"SELF CONTAINS %@", arrayItem]; NSLog(@"Filtered array with filter %@, %@", arrayItem, [arrayContents filteredArrayUsingPredicate:filterPredicate]); } |
当然以上代码中arrayContent最好用mutable 的,这样就可以直接filter了,NSArray是不可修改的。
2)例子二,无需循环
NSArray *arrayFilter = [NSArray arrayWithObjects:@"abc1", @"abc2", nil]; NSArray *arrayContent = [NSArray arrayWithObjects:@"a1", @"abc1", @"abc4", @"abc2", nil]; NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter]; [arrayContent filterUsingPredicate:thePredicate]; |
这样arrayContent过滤出来的就是不包含 arrayFilter中的所有item了。
3)生成文件路径下文件集合列表
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *defaultPath = [[NSBundle mainBundle] resourcePath];
NSError *error;
NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:defaultPath error:&error]
4)match的用法
1. 简单比较
NSString *match = @"imagexyz-999.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];?
2. match里like的用法(类似Sql中的用法)
NSString *match = @"imagexyz*.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];?
3. 大小写比较
[c]表示忽略大小写,[d]表示忽略重音,可以在一起使用,如下:
NSString *match = @"imagexyz*.png";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];?
4.使用正则
NSString *match = @"imagexyz-\\d{3}\\.png"; //imagexyz-123.png
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
NSArray *results = [directoryContents filteredArrayUsingPredicate:predicate];?