NSPredicate用法

筛选用法
NSArray

  • (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate;

NSMutableArray

  • (void)filterUsingPredicate:(NSPredicate *)predicate;

自身属性
NSArray *array = @[@"jim", @"cook", @"jobs", @"sdevm"]; NSPredicate *pre = [NSPredicate predicateWithFormat:@"length > 3"]; NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);

字符串相关
CONTAINS(包含)
NSArray *stringArray = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai",@"guangzou",@"wuhan", nil]; NSString *string = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string]; NSLog(@"%@",[stringArray filteredArrayUsingPredicate:pred])

SELF CONTAINS
表示自身包含
[c] 忽略大小写[d] 忽略重音符号[cd]既不区分大小写,也不区分发音符号。

[cd]的用法
NSArray *stringArray = [[NSArray alloc]initWithObjects:test1,test2,test3, nil]; NSString *targetString = @"Ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@",targetString]; NSLog(@"%@",[stringArray filteredArrayUsingPredicate:pred]);

上述打印都是相同的结果
2013-11-24 14:01:49.324 Nurse[5044:70b] ( shanghai, guangzou)

CONTAINS也可以用于自定义对象中
NSArray *stringArray = [[NSArray alloc]initWithObjects:test1,test2,test3, nil]; NSString *targetString = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"title CONTAINS %@",targetString]; NSLog(@"%@",[stringArray filteredArrayUsingPredicate:pred]);

title CONTAINS
title就是Test对象中的一个属性
BEGINSWITH(已某个字符串开头, begins with)
NSString *targetString = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"title BEGINSWITH %@",targetString];

ENDSWITH(已某个字符串结尾, ends with)
NSString *targetString = @"ang"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"title ENDSWITH %@",targetString];

比较运算符
符号
使用

==
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID == %@",@12ll];

!=
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID != %@",@1];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID > %@",@12ll];

<
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID < %@",@1];

范围运算符
符号
解释
使用

IN
之中
NSString
类型,判断title属性是否是字符串@"angle"和@"addss"中的一个:NSPredicate *pred = [NSPredicate predicateWithFormat:@"title IN {'angle', 'addss'}"];

IN
之中
NSNumber
类型,判断testID属性是否是NSNumber对象中的@1和@13中的一个:NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID IN {1, 13}"]

BETWEEN
之间
{1, 13}包括1和13:NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID BETWEEN {1, 13}"]

通配符 LIKE(也可以接[cd])
符号
解释
使用
备注

代表一个或者多个或者是空
NSPredicate pred = [NSPredicate predicateWithFormat:@"title LIKE 'ang*'"];

所以angle和lenang都符合

?

代表一个字符
NSPredicate pred = [NSPredicate predicateWithFormat:@"title LIKE '?ng'"];

所以只有 angle符合

AND(或&&)
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID >= %@ AND testID <=%@", @1ll, @13ll];


NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID BETWEEN {1, 13}"];

同样的效果
OR(或||)
NSPredicate *pred = [NSPredicate predicateWithFormat:@"title == 'angle' OR title == 'lenang'"];


NSPredicate *pred = [NSPredicate predicateWithFormat:@"title IN {'angle', 'addss'}"];

有同样的效果
NOT(或!)
从一个数组中过滤掉另外一个数组的所有数据
NSArray *arrayFilter = @[@"abc1", @"abc2"]; NSArray *arrayContent = @[@"a1", @"abc1", @"abc4", @"abc2"]; NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter]; NSLog(@"%@",[arrayContent filteredArrayUsingPredicate:thePredicate]);

指定字段过滤
NSPredicate *pred = [NSPredicate predicateWithFormat:@" NOT (testID IN %@ )",@[@1, @2]];

匹配用法

  • (BOOL)evaluateWithObject:(id)object;

Block

  • (NSPredicate*)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block

NSArray *array = @[@"jim", @"cook", @"jbos"]; NSPredicate *pre = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { return [[evaluatedObject valueForKey:@"name"] isEqualToString:@"cook"]; }]; NSArray *aray = [array filteredArrayUsingPredicate:pre];

block中第二个参数代表其实就是需要筛选的数组对象的其中一个成员,第二个参数至今没搞清楚作用

推荐阅读更多精彩内容