NSPredicate使用(3)——逻辑运算

NSArray *testArray = @[@1,@2,@3,@4,@5,@6];
// AND、&&:逻辑与,要求两个表达式的值都为YES时,结果才为YES。
nspredicate *predicate = [nspredicate predicateWithFormat:@"SELF > 2 && SELF < 5"];
NSArray *filterarray = [testArray filteredArrayUsingPredicate:predicate];
NSLog(@"元素值大于2,且小于5的数组:%@",filterarray);
// OR、||:逻辑或,要求其中一个表达式为YES时,结果就是YES
predicate = [nspredicate predicateWithFormat:@"SELF < 2 || SELF > 5"];
filterarray = [testArray filteredArrayUsingPredicate:predicate];
NSLog(@"元素值小于2,或大于5的数组:%@",filterarray);
// NOT、 !:逻辑非,对原有的表达式取反
predicate = [nspredicate predicateWithFormat:@"NOT (SELF > 2 AND SELF < 5)"];
filterarray = [testArray filteredArrayUsingPredicate:predicate];
NSLog(@"元素值既不大于2也不小于5,即小于等于2,或大于等于5:%@",filterarray);

运行结果

2018-03-05 15:32:44.268 Demonspredicate[2472:315182] 元素值大于2,且小于5的数组:(
    3,4
)
2018-03-05 15:32:44.268 Demonspredicate[2472:315182] 元素值小于2,或大于5的数组:(
    1,6
)
2018-03-05 15:32:44.269 Demonspredicate[2472:315182] 元素值既不大于2也不小于5,即小于等于2,或大于等于5:(
    1,2,5,6
)
2018-03-05 15:32:44.269 Demonspredicate[2472:315182] 元素值大于5的数组:(
    6
)

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...