• 谓词基本使用


     1 #pragma mark - 谓词基本使用
     2 - (void)demo1
     3 {
     4     // 谓词的用处是用来判断对象是否符合某种要求
     5     // predicateWithFormat指定谓词匹配的条件
     6     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS 'ao'"];
     7     
     8     NSString *str = @"hello";
     9     
    10     // 用谓词来匹配字符串
    11     if ([predicate evaluateWithObject:str]) {
    12         NSLog(@"符合条件");
    13     } else {
    14         NSLog(@"不符合条件");
    15     }
    16 }
    17 
    18 #pragma mark - 匹配数组
    19 - (void)demo2
    20 {
    21     // 准备数据
    22     NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:30];
    23     
    24     for (NSInteger i = 0; i < 30; i++) {
    25         NSString *name = [NSString stringWithFormat:@"ID:%04d", arc4random_uniform(10000)];
    26         NSInteger age = arc4random_uniform(20) + 15;
    27         
    28         Person *p = [Person personWithName:name age:age];
    29         
    30         [arrayM addObject:p];
    31     }
    32     // 需求:
    33     // 1> 姓名中包含"8"字符串的名字
    34     // 2> 查询年龄范围在 18 和 22 之间的
    35     // CONTAINS[cd]在匹配字符串时,不区分大小写,不区分注音符号
    36 //    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age BETWEEN {18, 22} && name CONTAINS[cd] '8'"];
    37 
    38     // 使用谓词对对象进行匹配时,是通过KVC进行匹配的,指定keyPath,需要用%K
    39     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS '8'", @"name"];
    40     
    41     // 对数组进行过滤
    42     NSLog(@"匹配结果 %@", [arrayM filteredArrayUsingPredicate:predicate]);
    43 }
  • 相关阅读:
    示例 json with js
    JS json
    jquery
    发布包规范
    20180320待编辑
    CefSharp中c#和js交互
    列举mvc ActionResult的返回值
    怎么实现第三方登录
    mvc @Html控件
    MVC webuploader 图片
  • 原文地址:https://www.cnblogs.com/orzmj123/p/3512194.html
Copyright © 2020-2023  润新知