zoukankan      html  css  js  c++  java
  • NSPredicate谓词

    //谓词,指定过滤器的条件,将符合条件的对象保留下来

     //一般用谓词过滤数组中指定的元素  

    int main(int argc, const charchar * argv[]) {      

    @autoreleasepool {                   

    NSArray *persons = [NSArray arrayWithObjects:                         

     [Person personWithName:@"mac" andAge:20],                           

     [Person personWithName:@"1" andAge:30],                             

     [Person personWithName:@"2" andAge:40],                             

     [Person personWithName:@"3" andAge:50],                           

      [Person personWithName:@"4" andAge:60],                           

      [Person personWithName:@"5" andAge:70],                           

      [Person personWithName:@"6" andAge:20],                             

    [Person personWithName:@"7" andAge:40],                             

    [Person personWithName:@"8" andAge:60],                             

    [Person personWithName:@"9" andAge:40],                             

    [Person personWithName:@"0" andAge:80],                             

    [Person personWithName:@"10" andAge:90],                          

    [Person personWithName:@"1" andAge:20]];                   

    //年龄小于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);                   

    //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'"]; 

  • 相关阅读:
    SpringBoot 项目集成增强版 SwaggerKnife4j 附常见问题及解决方案
    ZooKeeper 06 ZooKeeper 的常用命令
    ZooKeeper 04 ZooKeeper 集群的节点为什么必须是奇数个
    TCP扫描增强器实现65000端口,10S完成,快准狠(Go语言编程)
    集群服务器的网络连接状态接入ELK(可视化操作)
    golang的ping检测主机存活
    Gin编写邮件告警接口(添加配置,项目拆分)
    Gin编写邮件接口(支持多人发送)
    Linux操作系统账号密码失效检测
    Rsyslog同步集群服务器的网络连接状态
  • 原文地址:https://www.cnblogs.com/wujie123/p/5339128.html
Copyright © 2011-2022 走看看