美文网首页
NSPredicate使用

NSPredicate使用

作者: 幕夜丶席 | 来源:发表于2018-08-25 23:19 被阅读7次

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

1.对数组中是 Model``NSDictionary 过滤

定义模型 - CXPersion

@interface CXPerson : NSObject

@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;

@end

@implementation CXPerson

@end

tmp数组创建

NSMutableArray *tmp = [NSMutableArray array];
for (int i = 0; i < 20; i ++) {
    CXPersion *p = [CXPerson new];
    p.name = [NSString stringWithFormat:@"value%d", i];
    p.age = arc4random_uniform(210);
    [tmp addObject:p];
}

过滤Model中指定String

// name 是 key, value2 是 value
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = 'value2'"];
// array 是 tmp 所有 name = value2 的集合
NSArray *array = [tmp filteredArrayUsingPredicate:predicate];

过滤Model中指定条件
逻辑运算符号 > < = >= <= 都可以使用

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age < 100"];
NSArray *array = [tmp filteredArrayUsingPredicate:predicate];

&& || AND OR同理, 同时谓词不区分大小写

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name > 'abc' && age > 10"];

IN``BEGINSWITH ENDSWITH CONTAINS LIKE 的使用

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN {'abc' , 'def' , '123'}"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'A'"]; //name以A打头的person
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'A'"]; //name以A结尾的person
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'A'"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE '*A*'"];

2.对数组中是 String 过滤
使用 SELF

NSArray *array = [NSArray arrayWithObjects:@"abc", @"def", @"ghi", @"jkl", nil];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF == 'abc'"];
NSArray *array2 = [array filteredArrayUsingPredicate:pre];

3.对正则表达式的使用就不具体说明了

NSString *regex = @"";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
[predicate evaluateWithObject:@""];

更多用法就以后慢慢补充了...

相关文章

  • 排序进阶(NSPredicate、NSSortDescripto

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

  • [OC基础]NSPredicate

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

  • 使用NSPredicate

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

  • iOS 中的特殊类

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

  • NSPredicate使用

    NSPredicate 的使用

  • NSPredicate使用

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

  • NSPredicate使用

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

  • NSPredicate使用

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

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

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

  • NSPredicate的使用

    NSPredicate是一个Foundation类,它指定数据被获取或者过滤的方式。它的查询语言就像SQL的WHE...

网友评论

      本文标题:NSPredicate使用

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