[oc] NSPredicate的用法 【转】

 

一般来说这种情况还是蛮多的,比如你从文件中读入了一个array1,然后想把程序中的一个array2中符合array1中内容的元素过滤出来。

正 常傻瓜一点就是两个for循环,一个一个进行比较,这样效率不高,而且代码也不好看。

其实一个循环或者无需循环就可以搞定了,那就需要用搞 NSPredicate这个类了~膜拜此类~

开始之前要知道

NSArray filteredArrayUsingPredicate:NSPredicate]  返回一个新的过滤后的数组

[NSMutableArray filterUsingPredicate: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 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 = [NSFileManagerdefaultManager];
    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];
 

总结:

1) 当使用聚合类的操作符时是可以不需要循环的

2)当使用单个比较类的操作符时可以一个循环来搞定

PS,例子 一中尝试使用[@"SELF CONTAINS %@", arrayFilter] 来过滤会挂调,因为CONTAINS时字符串比较操作符,不是集合操作符。

 

 

 Human *human = [[Human alloc]init];  

        Human *child = [[Human alloc]init];  

        [human setValue:@"holydancer" forKey:@"name"];  

        [human setValue:[NSNumber numberWithInt:20] forKey:@"age"];  

        [human setValue:child forKey:@"child"];  

        [human setValue:[NSNumber numberWithInt:5] forKeyPath:@"child.age"];  

          

        NSPredicate *predicate1=[NSPredicate predicateWithFormat:@"name=='holydancer'"];//创建谓词判断属性  

        NSPredicate *predicate2=[NSPredicate predicateWithFormat:@"child.age==5"];//创建谓词判断属性的属性  

        //此处在创建谓词时可以有好多种条件写法,比如大小比较,范围验证,甚至像数据库操作那样的like运算符,这里就不一一列举了  

          

        BOOL tmp1=[predicate1 evaluateWithObject:human];//验证谓词是否成立,得到布尔返回值  

        BOOL tmp2=[predicate2 evaluateWithObject:human];  

        if (tmp1) {  

            NSLog(@"human对象的name属性为'holydancer'");  

        }  

        if (tmp2) {  

            NSLog(@"human对象的child属性的age为5");  

  

        }  

          

  } 

 
posted @ 2013-03-26 10:48  金建彤  阅读(364)  评论(0编辑  收藏  举报