ios 谓词

来源:互联网 发布:流程的再造与优化 编辑:程序博客网 时间:2024/06/16 00:13
  1.  //定义谓词对象,谓词对象中包含了过滤条件  
  2.         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
  3.         //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
  4.         NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
  5.         NSLog(@"filterArray=%@",array);  
  6.           
  7.         //查询name=1的并且age大于40  
  8.         predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
  9.         array = [persons filteredArrayUsingPredicate:predicate];  
  10.         NSLog(@"filterArray=%@",array);  
  11.           
  12.         //in(包含)  
  13.         predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];  
  14.           
  15.         //name以a开头的  
  16.         predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];  
  17.         //name以ba结尾的  
  18.         predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];  
  19.           
  20.         //name中包含字符a的  
  21.         predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];  
  22.           
  23.         //like 匹配任意多个字符  
  24.         //name中只要有s字符就满足条件  
  25.         predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];  
  26.         //?代表一个字符,下面的查询条件是:name中第二个字符是s的  
  27.         predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
0 0