zoukankan      html  css  js  c++  java
  • iOS OC 谓词筛选

     //年龄小于30  
            //定义谓词对象,谓词对象中包含了过滤条件  
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];  
            //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果  
            NSArray *array = [persons filteredArrayUsingPredicate:predicate];  
            NSLog(@"filterArray=%@",array);  
              
            //查询name=1的并且age大于40  与&&
            predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];  
            array = [persons filteredArrayUsingPredicate:predicate];  
            NSLog(@"filterArray=%@",array);  
        注:对于变量应使用
        predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@&& age>40",str];
      
              
            //in(包含)   或||
            predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];  
              
            //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'"];  
              
  • 相关阅读:
    问题:plugin with id 'android' not found
    问题:plugin with id 'android' not found
    【NYOJ】[122]Triangular Sums
    【NYOJ】[122]Triangular Sums
    【NYOJ】[113]字符串替换
    【NYOJ】[113]字符串替换
    【NYOJ】[111]分数加减法
    【NYOJ】[111]分数加减法
    【NYOJ】[101]两点距离
    【NYOJ】[101]两点距离
  • 原文地址:https://www.cnblogs.com/sunmair/p/7216713.html
Copyright © 2011-2022 走看看