NSPredicate谓词

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

比较运算符

/**比较运算符

* >:大于

* <:小于

* >=:大于等于

* <=:小于等于

* =,==:等于

* !=,<>:不等于

* between:左边的表达式等于右边的表达式的值或者介于它们之间。右边是一个有两个指定上限和下限的数值的数列(指定顺序的数列)。比如,1 BETWEEN { 0 , 33 },或者$INPUT BETWEEN { $LOWER, $UPPER }。

NSPRedicate*predicate = [NSPredicatepredicateWithFormat:@"age<%d",30];

NSArray*array = [personsfilteredArrayUsingPredicate:predicate];

in(包含)

name以a开头的

predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];

name以ba结尾的

predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];

name中包含字符a的

predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];

like 匹配任意多个字符

name中只要有s字符就满足条件

predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];

?代表一个字符,下面的查询条件是:name中第二个字符是s的

predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];

1)、过滤对象是数组:使用- (void)filterUsingPredicate:(NSPredicate *)predicate; 针对可变数组进行过滤,过滤掉可变数组中不符合条件的。-(NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate; 针对不可变数组进行过滤,将符合条件的元素组成一个新数组进行返回

2)、对单个对象进行判断过滤使用:- (BOOL)evaluateWithObject:(id)object; 向谓词对象发送该方法,参数是过滤的对象。常见于和正则表达式配合使用。

小结:以上三种方法都可以执行谓词短语,如果是对数组元素进行过滤的话,我们使用:- (void)filterUsingPredicate:(NSPredicate *)predicate、- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate;更方便,如果是只针对一个对象进行判断的话,使用- (BOOL)evaluateWithObject:(id)object; 更方便。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航