zoukankan      html  css  js  c++  java
  • NSPredicate类,指定过滤器的条件---董鑫

        /*
         比较和逻辑运算符
         就像前面的例子中使用了==操作符,NSPredicate还支持>, >=, <, <=, !=, <>,还支持AND, OR, NOT(或写成C语言样式&&, ||, ! ),其中AND,OR,NOT还是不区分大小写的
         */
        
        NSArray *array = [NSArray array];
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age <= 28"];
        for (Student *student in array) {
            //标示指定的对象是否满足谓语条件
            if ([predicate evaluateWithObject:student]) {
                NSLog(@"person name : %@",student.name);
            }
        }
        
        //返回一个符合谓语条件的数组
        NSArray *newArray = [array filteredArrayUsingPredicate:predicate];
        for (Student *student in newArray) {
            NSLog(@"person name : %@",student.name);
        }
        
        /*
         关键字 IN,BEGINSWITH,ENDSWITH,CONTAINS,LIKE,大小写都可以.添加的限制性字符串一定要添加''
         */
        Student *student = [[Student alloc]init];
        
        NSPredicate *preIN = [NSPredicate predicateWithFormat:@"student.name IN {'rose','bruse'}"];
        NSArray *student1 = [array filteredArrayUsingPredicate:preIN];
        //以**开始beginswith
        NSPredicate *preBeginsWith = [NSPredicate predicateWithFormat:@"student.name beginswith '滚犊子'"];
        NSArray *student2 = [array filteredArrayUsingPredicate:preBeginsWith];
        //以**结尾endswith
        NSPredicate *preEndsWith = [NSPredicate predicateWithFormat:@"student.name endswith '滚犊子'"];
        NSArray *student3 = [array filteredArrayUsingPredicate:preEndsWith];
        //包含contains
        NSPredicate *preContains = [NSPredicate predicateWithFormat:@"student.name contains '滚犊子'"];
        NSArray *student4 = [array filteredArrayUsingPredicate:preContains];
        //模糊查询like
        NSPredicate *preLike = [NSPredicate predicateWithFormat:@"student.name like '*%@*'",@"滚犊子"];
        NSArray *student5 = [array filteredArrayUsingPredicate:preLike];

  • 相关阅读:
    基于 HTML5 + WebGL 的 3D 风力发电场
    基于HTML5 WebGL的工业化3D电子围栏
    基于 HTML5 WebGL 和 VR 技术的 3D 机房数据中心可视化
    基于 HTML5 Canvas 的 Web SCADA 组态电机控制面板
    基于 HTML5 WebGL 与 WebVR 3D 虚拟现实的可视化培训系统
    基于 HTML5 WebGL 的 3D 服务器与客户端的通信
    什么是 SUID, SGID 和 Sticky bit
    贝塞尔曲线切割圆角
    iOS-获取当前View所在的控制器
    block(八)作用域
  • 原文地址:https://www.cnblogs.com/sixindev/p/4819775.html
Copyright © 2011-2022 走看看