NSPredicate简述

NSPredicate是iOS中用于定义逻辑条件的类,常用于数据获取和内存数据的筛选。本文涵盖过滤数值、字符串及数组、Set的方法,如数值相等、范围判断,字符串开头、结尾检查,正则匹配,以及数组模型的筛选等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NSPredicate简述

前言

NSPredicate是一个逻辑条件的定义,这个逻辑条件用来约束一个搜索条件,而这个搜索条件用于数据的获取或内存中数据的过程。仅作于笔记,如有侵权,联系删。

正文

一、过滤数值

1.判断数值相等,大小等


    NSNumber *num0 = @123;
    NSNumber *num1 = @1234;
    NSString *str0 = @"123";
    
    NSPredicate *pre0 = [NSPredicate predicateWithFormat:@"SELF = 123"];
    NSLog(@"%d",[pre0 evaluateWithObject:num0]);//1
    NSLog(@"%d",[pre0 evaluateWithObject:num1]);//0
    NSLog(@"%d",[pre0 evaluateWithObject:str0]);//0
    
    NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"SELF > 1000"];
    NSLog(@"%d",[pre1 evaluateWithObject:num0]);//1
    NSLog(@"%d",[pre1 evaluateWithObject:num1]);//0
    

2.判断数值范围Between


    NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"SELF BETWEEN {100, 200}"];
    NSLog(@"%d",[pre2 evaluateWithObject:num0]);//1
    NSLog(@"%d",[pre2 evaluateWithObject:num1]);//0
    
二、过滤字符串

1.BEGINWITH:是否以指定字符串开头

    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'a'"];
    
    NSLog(@"%d",[pre evaluateWithObject:@"ab"]);//1
    NSLog(@"%d",[pre evaluateWithObject:@"abc"]);//1
    NSLog(@"%d",[pre evaluateWithObject:@"ba"]);//0
    

2.ENDSWITH:是否以指定字符串结尾


NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF ENDSWITH 'ba'"];
    
    NSLog(@"%d",[pre evaluateWithObject:@"ab"]);//0
    NSLog(@"%d",[pre evaluateWithObject:@"abc"]);//0
    NSLog(@"%d",[pre evaluateWithObject:@"ba"]);//1
    NSLog(@"%d",[pre evaluateWithObject:@"cba"]);//1

3.CONTAINS:是否包含指定字符串


    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'jo'"];
    
    NSLog(@"%d",[pre evaluateWithObject:@"John"]);//0
    NSLog(@"%d",[pre evaluateWithObject:@"john"]);//1
    NSLog(@"%d",[pre evaluateWithObject:@"Milly"]);//0

4.LIKE & ?:是否可以匹配指定字符串模版

4.1 ?代表一个字符

NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF LIKE '?ng'"];
    NSLog(@"%d",[pre evaluateWithObject:@"png"]);//1
    NSLog(@"%d",[pre evaluateWithObject:@"jpng"]);//0


4.2 * 代表任意多个字符

NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF LIKE 'ang*'"];
    
    NSLog(@"%d",[pre evaluateWithObject:@"ango"]);//1
    NSLog(@"%d",[pre evaluateWithObject:@"angel"]);//1
    NSLog(@"%d",[pre evaluateWithObject:@"jiang"]);//0

5. MATCHES:是否匹配指定的正则表达式

- (void)checkPhoneNumber
{
    NSString *regex = @"^[1][3-8]\\d{9}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    NSLog(@"%d", [pred evaluateWithObject:@"15818663297"]);//1
    NSLog(@"%d", [pred evaluateWithObject:@"303178203"]);//0
}

6.字符串比较都是区分大小写和重音符的

如:café和cafe是不一样的,Cafe和cafe也是不一样的。判断时可以加上下面符号进行区分:

[c] 忽略大小写;
[d] 忽略重音符号;比如:name LIKE[cd] ‘cafe’,那么不论name是cafe、Cafe还是café上面的表达式都会返回YES。
[cd]既不区分大小写,也不区分发音符号。


NSArray *stringArray = @[@"John",@"john",@"Jolly",@"Milly"];
    NSString *targetString = @"jo";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@",targetString];
    NSLog(@"%@",[stringArray filteredArrayUsingPredicate:pred]);// John,john,Jolly

7.IN运算


对于 NSString类型,判断title属性是否是字符串@"angle"@"addss"中的一个:

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF IN {'angle', 'addss'}"];
    NSLog(@"%d", [pred evaluateWithObject:@"angle"]);//1
    NSLog(@"%d", [pred evaluateWithObject:@"blue"]);//0
    

对于 NSNumber类型,判断testID属性是否是NSNumber对象中的@1@13中的一个:

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF IN {1, 13}"];
    
    NSLog(@"%d", [pred evaluateWithObject:@(13)]);//1
    NSLog(@"%d", [pred evaluateWithObject:@(5)]);//0
    

三、过滤数组、Set

类型过滤方法
NSArrayfilteredArrayUsingPredicate:
NSMutableArrayfilterUsingPredicate:
NSSetfilteredSetUsingPredicate:
NSMutableSetfilterUsingPredicate:

1、数值数组筛选


    NSArray *testArray = @[@1, @2, @3, @4, @5, @6];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF > 2 && SELF < 5"];
    NSArray *filterArray = [testArray filteredArrayUsingPredicate:predicate];
    NSLog(@"filterArray:%@", filterArray);//3,4
    

2、字符串数组的筛选

- (void)testPre1{
    NSArray *testArray = @[@"JOLLY",@"billy", @"John", @"Joe", @"Tiffany", @"Mary", @"David"];
    //筛选字符长度大于4的元素
    NSPredicate *pre0 = [NSPredicate predicateWithFormat:@"SELF.length > 4"];
    NSArray *filterArray0 = [testArray filteredArrayUsingPredicate:pre0];
    
    NSLog(@"filterArray0:%@", filterArray0);//billy,Tiffany,David
    
    //取出包含‘Jo’的元素
    NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'Jo'"];
    NSArray *filterArray1 = [testArray filteredArrayUsingPredicate:pre1];
    NSLog(@"filterArray1:%@", filterArray1);//John,Joe
    
    NSString *keyword = @"Jo";
    NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"SELF.lowercaseString CONTAINS '%@'",keyword.lowercaseString];
    
    NSArray *filterArray2 = [testArray filteredArrayUsingPredicate:pre2];
    NSLog(@"filterArray2:%@", filterArray2);//JOLLY,John,Joe
}

3、取两个数组的交集

NSArray *array = @[@"a", @"ab", @"abc", @"abcd"];
    NSArray *subArray = @[@"ab", @"abc",@"ab1"];
    
    NSPredicate *pre0 = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", subArray];//不包含在subArray的元素
    NSLog(@"0-%@", [array filteredArrayUsingPredicate:pre0]);//a,abcd
    
    
    NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"SELF IN %@", subArray];//包含在subArray的元素
    NSLog(@"1-%@", [array filteredArrayUsingPredicate:pre1]);//ab,abc


4.过滤模型数组

@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *sex;
@property (nonatomic,assign) NSInteger age;
-(instancetype)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age;

创建测试数据:

    PersonModel *model0 = [[PersonModel alloc]initWithName:@"joe" sex:@"female" age:18];
    PersonModel *model1 = [[PersonModel alloc]initWithName:@"jim" sex:@"female" age:20];
    PersonModel *model2 = [[PersonModel alloc]initWithName:@"jone" sex:@"female" age:23];
    PersonModel *model3 = [[PersonModel alloc]initWithName:@"milly" sex:@"female" age:25];
    PersonModel *model4 = [[PersonModel alloc]initWithName:@"Elaine" sex:@"female" age:28];
    
    NSArray *array = @[model0,model1,model2,model3,model4];

筛选名字包含 jo 的模型, 下面四种方式搜索结果相同:

    NSPredicate *pred0 = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", @"name", @"jo"];
    NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"jo"];
    NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF.name CONTAINS %@", @"jo"];
    NSPredicate *predTemp3 = [NSPredicate predicateWithFormat:@"%K CONTAINS $VALUE", @"name"];
    NSPredicate* pred3 = [predTemp3 predicateWithSubstitutionVariables:@{@"VALUE" : @"jo"}];
    
    NSLog(@"%@", [array filteredArrayUsingPredicate:pred0]);
    NSLog(@"%@", [array filteredArrayUsingPredicate:pred1]);
    NSLog(@"%@", [array filteredArrayUsingPredicate:pred2]);
    NSLog(@"%@", [array filteredArrayUsingPredicate:pred3]);
   



其中

  • 属性作为key,可以用 %K 表示。
  • 使用 $ 修饰的字符,可以作为参数,在 predicateWithSubstitutionVariables
    中使用字典的形式赋值;在其他时候赋值,也方便产生多个条件类似的过滤器。Value
    字符也可以替换为其他字符,只要前后统一即可,最好不要用关键字。

下面示例同样使用到这个方法,方便你的理解:
predicateWithSubstitutionVariables

    // 创建谓词,属性名改为age,要求这个age包含$VALUE字符串
    NSPredicate *predTemp4 = [NSPredicate predicateWithFormat:@"%K > $VALUE", @"age"];
    // 指定$VALUE的值为 20
    NSPredicate *pred4 = [predTemp4 predicateWithSubstitutionVariables:@{@"VALUE" : @20}];
    NSLog(@"%@", [array filteredArrayUsingPredicate:pred4]);//筛选到年级大于20的模型


附上原文
iOS NSPredicate 使用详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值