[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");  

  

        }  

          

  } 

 

转载于:https://www.cnblogs.com/jinjiantong/archive/2013/03/26/2982071.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值