美文网首页
NSPredicate使用笔记

NSPredicate使用笔记

作者: Onego | 来源:发表于2017-06-20 14:11 被阅读91次

1.常用方法

//    1.创建NSPredicate(相当于创建一个过滤条件)
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"过滤条件"];
//    2.判断指定的对象是否满足NSPredicate创建的过滤条件
    [predicate evaluateWithObject:person];
//    3.过滤出符合条件的对象(返回所有符合条件的对象)
    NSArray *persons = [array filteredArrayUsingPredicate:predicate];
    NSArray *arrPersons = @[
                          [[Person alloc] initWithName:@"Mark" gender:@"M" age:36],
                          [[Person alloc] initWithName:@"Andy" gender:@"F" age:29],
                          [[Person alloc] initWithName:@"Jack" gender:@"M" age:20],
                          [[Person alloc] initWithName:@"Jobs" gender:@"M" age:59],
                          [[Person alloc] initWithName:@"Elizabeth" gender:@"F" age:81],
                          ];
    ```

2.比较运算符 >,<,==,>=,<=,!=,AND,&&,OR,||
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age>50 AND gender='F'"]; // 注意这个语法里面字符串要用单引号
NSArray *result = [arrPersons filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
 输出结果:
 (
 "Elizabeth-F-81"
 )
 */

3.范围运算符,in(则表示在后面的{}中是否包含),between(在某个范围内,如:age between{15,25}等价于 15<=age<=25

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age between{20,30}"];
NSArray *result = [arrPersons filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
 输出结果:
 (
 "Andy-F-29",
 "Jack-M-20"
 )
 */


//场景3
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age in{20,30}"];
NSArray *result = [arrPersons filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
 输出结果:
 (
 "Jack-M-20"
 )
 */

//场景4
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in{'Jobs','Mark'}"]; //当条件是NSString时 in 和 between 结果一致
NSArray *result = [arrPersons filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
 输出结果:
 (
 "Mark-M-36",
 "Jobs-M-59"
 )
 */

4.对象本身self 如@"SELF == 'Apple'"(也可以是NSNumber)


NSArray *arr = @[@"Apple",@"Mac",@"iPhone"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == 'Apple'"];

NSArray *result = [arr filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
 输出结果:
 (
 Apple
 )
 */
5.字符串相关

:BEGINSWITH、ENDSWITH、CONTAINS
例:@"name CONTAINS[cd] 'ang'" //包含某个字符串
@"name BEGINSWITH[c] 'sh'" //以某个字符串开头
@"name ENDSWITH[d] 'ang'" //以某个字符串结束
//场景6
NSArray *arr = @[@"yangjie",@"zhangsan",@"lisi"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[cd] 'ng'"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self beginswith[c] 'y'"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self endswith[d] 'i'"];
6.通配符:LIKE

// 例:@"name LIKE[cd] 'er'" //代表通配符,Like也接受[cd].
// @"name LIKE[cd] '???er'"
NSArray *arr = @[@"yangjie",@"zhangsan",@"lisi",@"ang",@"tangddd",@"fangxxx"];
NSPredicate predicate = [NSPredicate predicateWithFormat:@"self like[cd] 'ang'"];
// 注意这个语法里面字符串要用单引号

NSArray *result = [arr filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
// 打印输出结果
NSLog(@"%@",result);
/*
 输出结果:
 (
 ang
 )
 */

7.正则表达式:MATCHES

NSPredicate 使用MATCHES 匹配正则表达式,正则表达式的写法采用international components
for Unicode (ICU)的正则语法。


例:NSString *regex = @"^A.+e$"; //以A开头,e结尾
@"name MATCHES %@",regex

        例:
    NSString *regex = @"^A.+e$";//以A 开头,以e 结尾的字符。
NSPredicate *pre= [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if([pre evaluateWithObject: @"Apple"]){
    printf("YES\n");
}else{  
    printf("NO\n");  
}

相关文章

  • 排序进阶(NSPredicate、NSSortDescripto

    一、NSPredicate的使用 1、> 的使用 NSPredicate*predicate = [NSPredi...

  • [OC基础]NSPredicate

    NSPredicate,用来指定过滤器条件的对象 创建 NSPredicate 对象 使用 NSPredicate...

  • 使用NSPredicate

    使用简单的谓词: NSPredicate *predicate = [NSPredicate predicated...

  • iOS 中的特殊类

    NSPredicate NSPredicate 是预测的意思,但我们常翻译成谓词。 使用NSPredicate可以...

  • NSPredicate使用笔记

    1.常用方法 // 例:@"name LIKE[cd] 'er'" //代表通配符,Like也接受[cd]....

  • NSPredicate使用

    NSPredicate 的使用

  • NSPredicate使用

    之前对NSPredicate了解过,但是过滤有一段时间给忘记了..这次记录一下,省的忘记 1.对数组中是 Mode...

  • NSPredicate使用

    谓词(NSPredicate) OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这...

  • NSPredicate使用

    NSPredicate是一个Foundation类,它指定数据被获取或者过滤的方式。 NSPredicate使用时...

  • iOS开发中NSPredicate(谓词)的简单使用

    说实话之前还没有在开发中直接使用过NSPredicate。NSPredicate可以实现模糊搜索的功能,如果一个数...

网友评论

      本文标题:NSPredicate使用笔记

      本文链接:https://www.haomeiwen.com/subject/kjeoqxtx.html