何为“谓词”,即大家所熟悉的NSPredicate。
谓词表达式
说到谓词,少不了谓词表达式,谓词表达式有三个部分组成:表达式、运算符、返回值。谓词表达式必须返回一个BOOL类型的值。
那它能为我们做些什么呢?接下来开始一一道来。
谓词定义
常用的谓词定义方法有两种:
[NSPredicate predicateWithFormat:@""]
[NSPredicate predicateWithBlock:(BOOL (^)(id _Nullable evaluatedObject, NSDictionary<NSString *, id> * _Nullable bindings))block
predicateWithBlock需要在block中写比较逻辑。
例如:
Model *model0 = [self createModel:@"jane" age:10];
Model *model1 = [self createModel:@"tim1" age:20];
Model *model2 = [self createModel:@"tim2" age:21];
Model *model3 = [self createModel:@"tim3" age:22];
Model *model4 = [self createModel:@"tim4" age:23];
Model *model5 = [self createModel:@"Tim6" age:11];
NSArray *array = @[model0,model1,model2,model3,model4,model5];
NSLog(@"filter0:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.age BETWEEN {10,15} AND SELF.name BEGINSWITH[cd] %@",@"tim"]]);
NSLog(@"filter00:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.age BETWEEN {10,15} AND SELF.name LIKE[cd] %@",@"tim*"]]);
NSLog(@"filter1:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.age BETWEEN {10,15} AND SELF.name CONTAINS[cd] %@",@"tim"]]);
NSLog(@"filter11:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.age BETWEEN {10,15} AND SELF.name CONTAINS[cd] %@",@"tim"]]);
NSLog(@"filte2r:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.age >= 10 AND SELF.age <= 15"]]);
NSLog(@"filter3:%@",[array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(Model *evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
if (evaluatedObject.age >= 10 && evaluatedObject.age <= 15) {
return YES;
}
return NO;
}]]);
/*
filter0:(
"<Model: 0x6000037f2780>"
)
filter00:(
"<Model: 0x6000037f2780>"
)
filter1:(
"<Model: 0x6000037f2780>"
)
filter11:(
"<Model: 0x6000037f2780>"
)
filte2r:(
"<Model: 0x6000037f2720>",
"<Model: 0x6000037f2780>"
)
filter3:(
"<Model: 0x6000037f2720>",
"<Model: 0x6000037f2780>"
)
*/
谓词占位符
%K,%@
其中%K可以表示需要匹配的property,比如 name LIKE 'tim',
用占位符表示为%K LIKE %@,@"name",@"tim"
比较运算
关键字
-
=、==
表示两个表达式或值是否相等,比如:
NSPredicate *predicate11 = [NSPredicate predicateWithFormat:@"SELF = 'abc'"];
NSPredicate *predicate12 = [NSPredicate predicateWithFormat:@"SELF ==[cd] 'abc'"];
NSLog(@"结果11:%d",[predicate11 evaluateWithObject:str]);
NSLog(@"结果12:%d",[predicate12 evaluateWithObject:str]);
//结果11:0
//结果12:1
-
>,<,>=,<=,!=
大小比较 -
BETWEEN
表示在范围区间之间,比如:SELF BETWEEN {100,200} 等价于 SELF >= 100 && SELF <= 200.
逻辑运算
-
AND,&&
逻辑与 -
OR,||
逻辑或 -
NOT,!
逻辑非
字符串匹配
关键字:
-
BEGINSWITH
以某个字符开头,比如:判断是否以w字符开头,BEGINSWITH 'w' -
ENDSWITH
以某个字符结尾,比如:判断是否以w字符结尾,ENDSWITH 'w' -
CONTAINS
表示字符串中是否包含某特定字符串,比如:CONTAINS 'abc' -
LIKE
检测字符串是否匹配某字符串模板,比如:name LIKE '?a'。表示name是否为第二个字母为'a' -
MATCHES
用于匹配正则表达式,比如匹配手机号等:SELF MATCHES '^[1][3-8]\d{9}$'
其中可以用[c]紧接在关键字后表示忽略大小写匹配,[d]表示忽略重低音匹配,[cd]可以一起连用。比如:
NSString *str = @"aBc";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] 'b'"];
NSLog(@"结果:%d",[predicate evaluateWithObject:str]);
//结果:1
集合运算
关键字
-
ANY,SOME
集合中任意一个元素满足条件,就返回YES。 -
ALL
集合中所有元素满足条件,就返回YES。 -
NONE
集合中所有元素都满足条件,就返回YES。 -
IN
继承中IN左右的元素出现在表示式右边时才返回TRUE.
NSArray *arr1 = @[@1,@2,@3,@4,@5];
NSArray *arr2 = @[@1,@3,@5];
NSLog(@"filter1:%@",[arr1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF IN %@",arr2]]);
NSLog(@"filter2:%@",[arr1 filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT SELF IN %@",arr2]]);
/*
filter1:(
1,
3,
5
)
filter2:(
2,
4
)
*/
以上就是简单的介绍,谨以此加深印象。