NSPredicate的用法

一般来说这种情况还是蛮多的,比如你从文件中读入了一个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 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];

总结:

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

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

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

转:http://www.cnblogs.com/MarsGG/articles/1949239.html

NSPredicate的用法,布布扣,bubuko.com

时间: 2025-01-12 19:19:39

NSPredicate的用法的相关文章

NSPredicate的用法、数组去重、比较...

一般来说这种情况还是蛮多的,比如你从文件中读入了一个array1,然后想把程序中的一个array2中符合array1中内容的元素过滤出来. 1)例子一,一个循环 NSArray *arrayFilter = [NSArray arrayWithObjects:@"pict", @"blackrain", @"ip", nil]; NSArray *arrayContents = [NSArray arrayWithObjects:@"I

【学习ios之路:UI系列】NSPredicate相关用法

NSPredicate NSPredicate是一个Foundation类,它指定数据被获取或者过滤的方式. 它的查询语言就像SQL的WHERE和正则表达式的交叉一样,提供了具有表现力的,自然语言界面来定义一个集合被搜寻的逻辑条件. NSPredicate的几种用法 ①常见形式 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == 'zhangdan'"]; //注意:如果谓词串中的文本块未被引用,则被看

NSPredicate 的用法举例

Cocoa 提供了一个名为NSPredicate的类,用于指定过滤器的条件,用NSPredicate描述查询方式,原理类似于在数据库中进行查询.可以在数据库风格的API中使用NSPredicate类,常见的用于Core Data和Spotlight. 本文讲解过程中用到了部分类比如garage(车库的类),car(车)等仅供举例,未列出创建代码,体会predicate的用法才是关键. 创建Predicate. NSPredicate *predicate = [NSPredicate predi

iOS开发_常用的正则表达式

实现正则表达式匹配的方式 常用的正则表达式 实现正则表达式匹配的方式 iOS中有三种方式可以实现正则表达式的匹配. 1.利用NSPredicate(谓词)匹配 例如匹配有效邮箱: NSString *email = @"[email protected]"; // 邮箱的正则表达式 NSString *emailRegex = @"[A-Z0-9a-z._%+-][email protected][A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; //

CoreData用法三: NSPredicate在CoreData中的使用

NSPredicate在CoreData中常用作查询使用,相当于sql语句中的where查询子句. 最常用的方法为: NSPredicate *ca = [NSPredicate predicateWithFormat:(NSString *), ...]; 比如我们要查询student表中name="jjy"的信息,我们可以这样去用NSPredicate NSEntityDescription * emEty = [NSEntityDescription entityForName:

NSPredicate用法总结(Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取)

简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 定义(最常用到的方法): [objc] view plaincopy NSPredicate *ca = [NSPredicate predicateWithFormat:(NSString *), ...]; Format:(1)比较运算符>,<,==,>=,<=,!=可用于数值及字符串例:@"number > 100" (2)范围

IOS开发之NSPredicate谓词的用法

编程的人员不管是上过大学还是从培训机构出来的都应该SQL语句有所了解,我们知道,在SQL语句当中 where 条件表达式可以对二维关系表的数据做条件筛选.微软的C# .net中也实现了功能能和SQL语句相媲美的技术,它就是List泛型集合的Lambda表达式,支持查找.排序.比较.组合等.在java中虽然没有在语言中集成对List对象的操作的实现,但是第三方开源库同样实现了这一功能.在IOS开发Cocoa框架中提供了一个功能强大的类NSPredicate,下面来讨论一下它的强大之处在哪...NS

iOS开发,谓词(NSPredicate)的用法:(一)基本用法

<span style="white-space: pre;"> </span>在iOS开发中,系统提供了NSPredicate这个类给我们进行一些匹配.筛选操作,非常方便.在没有用这个类时,我们要获取两个数组中某些特定的元素时,需要写代码一一对比,但是使用了这个类,只需要三四行代码就够了. 为了演示,先定义一个person类 .h文件 #import <Foundation/Foundation.h> @interface Person : NSO

iOS之谓词(NSPredicate)(正则表达式和UIBarController)

iOS之NSPredicate(正则表达式和UIBarController) 本文转发至:https://segmentfault.com/a/1190000000623005 NSPredicate,这个类和我上一篇博文中提到的valueForKeyPath一样很强大.它的使用主要集中在两个方法中 NSArray - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; NSMutableArray - (void)f