iOS NSPredicate 谓词常用语法(精简版)

能看到这里说明你已经对NSPredicate有了一个基本的认识,这里不再赘述NSPredicate的定义,本文主要演示NSPredicate的基本用法
废话不多说,直接撸正体,喜欢就点个赞,不喜欢的可以留言指出问题,我会第一时间修改验证

先来两组NSArray的 API

// evaluate a predicate against an array of objects and return a filtered array
- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate;   

// evaluate a predicate against an array of objects and filter the mutable array directly
- (void)filterUsingPredicate:(NSPredicate *)predicate;    
Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;
Test *test2 = [[Test alloc]init];test2.name = @"asb";test2.code = @2;
Test *test3 = [[Test alloc]init];test3.name = @"raskj";test3.code = @3;

过滤生成新数据

NSArray *objArray = @[test1,test2,test3];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"name.length > %@",@3];
NSArray *filtArray = [objArray filteredArrayUsingPredicate:pred];

objArray 自身过滤

NSMutableArray *objArray = [NSMutableArray arrayWithArray:@[test1,test2,test3]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"name.length > %@",@3];
[objArray filterUsingPredicate:pred];

————————————————————————

自身属性做为过滤条件

NSArray Testarray = @[@"oc", @"swift", @"java", @"python"]; 
NSPredicate pre = [NSPredicate predicateWithFormat:@"length > 4"]; 
NSArray *filtArray = [Testarray filteredArrayUsingPredicate:pred];
#过滤结果为 @[python];

对象字段属性做为过滤条件

Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;
Test *test2 = [[Test alloc]init];test2.name = @"asb";test2.code = @2;
Test *test3 = [[Test alloc]init];test3.name = @"raskj";test3.code = @3;
NSArray *objArray = @[test1,test2,test3];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"name.length > %@",@3];
NSArray *filtArray = [objArray filteredArrayUsingPredicate:pred];
#过滤结果为 @[test1,test3];

字符串相关

CONTAINS(包含)
NSArray stringArray = [[NSArray alloc]initWithObjects:@"zizhi",@"chezi",@"reiki", nil]; 
NSString containStr = @"zi"; 
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", containStr]; 
NSArray *filtArray = [stringArray filteredArrayUsingPredicate: pre])

SELF CONTAINS

表示自身包含 SELF前面加NOT表示相反,切记是放在SELF前不是SELF后

[cd]的用法

[c] 忽略大小写[d] 忽略重音符号[cd]既不区分大小写,也不区分发音符号。

NSArray testArray = [[NSArray alloc]initWithObjects:test1,test2,test3, nil];
NSString targetString = @"Ang"; 
NSPredicate *pred = [NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@",targetString]; 
NSArray *filtArray = [testArray filteredArrayUsingPredicate:pred]);

CONTAINS 包含过滤

NSArray stringArray = [[NSArray alloc]initWithObjects:test1,test2,test3, nil]; 
NSString targetString = @"ang"; 
NSPredicate *pred = [NSPredicate predicateWithFormat:@"title CONTAINS %@",targetString];
NSArray *filtArray =[stringArray filteredArrayUsingPredicate:pred]);
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 == %@",@12];

!=

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

>

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

<

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 'da*'"];
字符串包含ang即  如“danche”、“zidan”

? 通配一个字符

NSPredicate pred = [NSPredicate predicateWithFormat:@"title LIKE '?ng'"];

“ing”通配 ;“jing”就不通配

AND(或&&)

NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID >= %@ AND testID <=%@", @11, @13];

OR(或||)

NSPredicate *pred = [NSPredicate predicateWithFormat:@"title == 'angle' OR title == 'lenang'"];

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
Test *test1 = [[Test alloc]init];
test1.name = @"absr";
test1.code = @1; 
NSPredicate *pres = [NSPredicate predicateWithFormat:@"code == %@", @2];    
BOOL match = [pres evaluateWithObject:test1];

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中第二个参数NSDictionary bindings我看好多文章都写不知道是干嘛的,这里就简单讲一下代表其实就是需要筛选的数组对象的其中一个成员,第二个参数是传过来的参数,如下代码中的@{@"$VALUES":@"name"},可以打个断点观察一下。


NSPredicate* predicateBlock = [NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary * _Nullable bindings) {
        NSLog(@"%@",bindings);
        return YES;
    }];
[predicateBlock evaluateWithObject:nil substitutionVariables:@{@"$VALUES":@"name"}];

组合过滤

 Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;
    Test *test2 = [[Test alloc]init];test2.name = @"asb";test2.code = @2;
    Test *test3 = [[Test alloc]init];test3.name = @"raskj";test3.code = @3;
    NSArray *array = @[test1,test2,test3];

    NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"code >= %@",@1];
    NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"code <= %@",@2];
    
    //以AND形式组合
    NSPredicate *andPre = [NSCompoundPredicate andPredicateWithSubpredicates:@[pre1,pre2]];
    
    //以OR形式组合
    NSPredicate *orPre = [NSCompoundPredicate orPredicateWithSubpredicates:@[pre1, pre2]];

//过滤条件以外的数据
    NSPredicate *notPre = [NSCompoundPredicate notPredicateWithSubpredicate:pre2];
    
    NSArray *filtArray = [array filteredArrayUsingPredicate:orPre];

关于 NSPredicate的用法还有好多好多,大家可以自己慢慢实践,本文也不会不定期的更新

你可能感兴趣的:(iOS NSPredicate 谓词常用语法(精简版))