NSPredicate:主要用来指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配。谓词是指在计算机中表示计算真假值的函数。原理和用法都类似于SQL查询中的where,作用相当于数据库的过滤取。主要用于从集合中分拣出符合条件的对象,也可以用于字符串的正则匹配
定义(最常用到的方法):
- NSPredicate
*ca = [NSPredicate predicateWithFormat:(NSString *), ...];
例子1:
(1)对NSArray进行过滤- NSArray
*array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil]; - NSString
*string = @"ang"; - NSPredicate
*pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string]; - NSLog(@"%@",[array
filteredArrayUsingPredic ate:pred]);
例子2:
- #import
- @interface
Person: NSObject{ -
int pid; -
NSString *name; -
float height; - }
- @end
- int
main(int argc, char *argv[]) - {
-
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; -
//实例化三个Person,并放入数组。 -
NSMutableArray *array=[NSMutableArray arrayWithCapacity: 5]; -
Person *person1=[[Person alloc] init]; -
[person1 setPid: 1]; -
[person1 setName: @"Name1"]; -
[person1 setHeight: 168]; -
[array addObject: person1]; -
Person *person2=[[Person alloc] init]; -
[person2 setPid: 2]; -
[person2 setName: @"Name2"]; -
[person2 setHeight: 178]; -
[array addObject: person2]; -
Person *person3=[[Person alloc] init]; -
[person3 setPid: 3]; -
[person3 setName: @"Name3"]; -
[person3 setHeight: 188]; -
[array addObject: person3]; -
//创建谓词,条件是pid>1 并且height<188.0。其实谓词也是基于KVC 的,也就是如果pid 在person 的成员变量xxx 中,那么此处要写成xxx.pid>1。 -
NSPredicate *pre = [NSPredicate predicateWithFormat: -
@" pid>1 and height<188.0"]; -
int i=0; -
for(;i<[array count];i++){ -
Person *person=[array objectAtIndex: i]; -
//遍历数组,输出符合谓词条件的Person 的name。 -
if ([pre evaluateWithObject: person]) { -
NSLog(@"%@",[person name]); -
} -
} -
[person1 release]; -
[person2 release]; -
[person3 release]; -
[pool release]; -
return 0; - }
创建谓词使用类方法predicateWithFormat: (NSString*) format,
format 里的东西真的和SQL 的where 条件差不多。
另外,参数format 与NSLog 的格式化模版差不多,如果1 和188.0 是传递过来的参数,你可以写成如下的形式:
@"pid > %d and height < %f",1,188.0
(1)比较运算符>,<,==
可用于数值及字符串
例:@"number > 100"
(1.) 逻辑运算符:AND、OR、NOT
这几个运算符计算并、或、非的结果。
(2.) 范围运算符:BETWEEN、IN
例:
@”pid BETWEEN {1,5}”
@"name IN {'Name1','Name2'}"
(3.) 占位符:
NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"name==$NAME"];
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAnd
@"Name1", @"NAME",nil];
NSPredicate *pre=[preTemplate predicateWithSubstitutio
占位符就是字段对象里的key,因此你可以有多个占位符,只要key 不一样就可以了。
(4.) 快速筛选数组:
前面我们都是使用谓词逐个判断数组内的对象是否符合,其实数组本身有更为便捷的方法,
直接筛选出一个符合谓词的新数组。
NSPredicate *pre = [NSPredicate predicateWithFormat:@"pid>1"];
NSMutableArray *arrayPre=[array filteredArrayUsingPredic
NSLog(@"%@",[[arrayPre objectAtIndex: 0] name]);
(5.) 字符串运算符:
BEGINSWITH、ENDSWITH、CONTAINS 分别表示是否以某字符串开头、结尾、包含。
他们可以与c、d 连用,表示是否忽略大小写、是否忽略重音字母(字母上方有声调标号)。
例:
@”name BEGINSWITH[cd] ‘He’”
判断name 是否以He 开头,并且忽略大小写、忽略重音字母。
(6.) LIKE 运算符:
LIKE 使用?表示一个字符,*表示多个字符,也可以与c、d 连用。
例:
@”name LIKE ‘???er*’” 与Paper Plane 相匹配。
(7.) SELF:
前面的数组中放的都是对象,如果数组放的都是字符串(或者是其他没有属性的类型),该怎么写谓词呢?这里我们使用SELF。
例:
NSArray *arrays=[NSArray arrayWithObjects: @"Apple", @"Google", @"MircoSoft", nil];
NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"SELF=='Apple'"];
(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");
}
例子3:
一般来说这种情况还是蛮多的,比如你从文件中读入了一个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]; |
我想过滤arrayContents的话只要循环 arrayFilter就好了
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 filteredArrayUsingPredic } |
当然以上代码中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了。
注:不能用[@"SELF CONTAINS %@", arrayFilter] 来过滤,因为CONTAINS时字符串比较操作符,不是集合操作符。
3)生成文件路径下文件集合列表
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *defaultPath = [[NSBundle mainBundle] resourcePath];
NSError *error;
NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:defaultPath error:&error]
4)match的用法
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];
总结:
1) 当使用聚合类的操作符时是可以不需要循环的
2)当使用单个比较类的操作符时可以一个循环来搞定
NSPredicate源代码:
- #import
- #import
- #import
-
- NS_CLASS_AVAILABLE(10_4,
3_0) - @interface
NSPredicate : NSObject { -
void *_reserved; - }
- +
(NSPredicate *)predicateWithFormat:(NSString *)predicateFormat argumentArray:(NSArray *)arguments; - +
(NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...; - +
(NSPredicate *)predicateWithFormat:(NSString *)predicateFormat arguments:(va_list)argList; -
- +
(NSPredicate *)predicateWithValue:(BOOL)value; -
- #if
NS_BLOCKS_AVAILABLE - +
(NSPredicate*)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block NS_AVAILABLE(10_6, 4_0); - #endif
-
- -
(NSString *)predicateFormat; -
- -
(NSPredicate *)predicateWithSubstitutio nVariables:(NSDictionary *)variables; -
- -
(BOOL)evaluateWithObject:(id)object; -
- -
(BOOL)evaluateWithObject:(id)object substitutionVariables:(NSDictionary *)bindings NS_AVAILABLE(10_5, 3_0); -
- @end
-
- @interface
NSArray (NSPredicateSupport) - -
(NSArray *)filteredArrayUsingPredic ate:(NSPredicate *)predicate; - @end
-
- @interface
NSMutableArray (NSPredicateSupport) - -
(void)filterUsingPredicate:(NSPredicate *)predicate; - @end
-
-
- @interface
NSSet (NSPredicateSupport) - -
(NSSet *)filteredSetUsingPredicat e:(NSPredicate *)predicate NS_AVAILABLE(10_5, 3_0); - @end
-
- @interface
NSMutableSet (NSPredicateSupport) - -
(void)filterUsingPredicate:(NSPredicate *)predicate NS_AVAILABLE(10_5, 3_0); - @end