谓词
思维导图
这里先建个类Person,以下都是基于这个类解说
@interface Person : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSUInteger age;
@property (nonatomic,copy) NSString *gender;
@property (nonatomic,strong) NSArray *skills;
@end
@implementation Person
+ (instancetype)personWithName:(NSString *)name age:(NSUInteger)age gender:(NSString *)gender{
Person *p = [[Person alloc]init];
p.name = name;
p.age = age;
p.gender = gender;
return p;
}
@end
- 单条件判断(数字、字符串)
数字相等判断
Person *p = [Person personWithName:@"Geroge" age:20 gender:@"male"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age == 20"];
if ([predicate evaluateWithObject:p]) {
NSLog(@"YES");
}else{
NSLog(@"NO");
}
字符串判断
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in {'Geroge'}"];
数值在某个范围内判断
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age between {18,22}"];
- 多条件判断 (与或非)
name为Geroge 并且 age等于20
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in {'Geroge'} AND age == 20"];
name为Geroge或者age等于20
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in {'Geroge'} OR age == 20"];
age不为20
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"not age == 20"];
- 不定向条件判断 (开头,结尾,包含,匹配)
name 以 Ger 开头判断
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith 'Ger'"];
name 以 ge 结尾判断
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name endswith 'ge'"];
name 包含 rog 判断
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains 'rog'"];
name 中是否有 Ger*匹配
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like 'Ger*'"];
- 判断集合属性是否含有某元素
如果设置skills属性,既加入一个数组,检查数组内是否有某类对象
p.skills = @[@"hello",@"world",@"yes",@"no"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"any skills like 'ye*'"];
判断是否全部都有某种属性
p.skills = @[@"hello",@"world",@"yes",@"no"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"all skills like 'ye*'"];
判断是否没有该属性
p.skills = @[@"hello",@"world",@"yes",@"no"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"none skills like 'ye*'"];
- 格式化谓词
格式化path 用 %K,此处K必须大写
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"any %K like '*e*l*o'",@"skills"];
格式化value用%@
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"any %K like %@",@"skills",@"*e*l*o"];
self的应用
NSString *str = @"hello world";
predicate = [NSPredicate predicateWithFormat:@"self beginswith %@",@"he"];
- 谓词的应用
在谓词的头文件中,加了array,dictionary,set的类别,根据所给定的predicate来过滤里面的元素,以数组为例
先创建5个Person对象,放到数组中
Person *p1 = [Person personWithName:@"Geroge" age:20 gender:@"male"];
Person *p2 = [Person personWithName:@"Amy" age:18 gender:@"female"];
Person *p3 = [Person personWithName:@"Tom" age:24 gender:@"male"];
Person *p4 = [Person personWithName:@"Andy" age:22 gender:@"male"];
Person *p5 = [Person personWithName:@"Rose" age:25 gender:@"female"];
NSArray *personArray = @[p1,p2,p3,p4,p5];
如果我们想把女性留下,男性都过滤掉,可做如下操作
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",@"gender",@"fe*"];
NSArray *newArray = [personArray filteredArrayUsingPredicate:predicate];
最终中,newArray中保留的是女性的Person成员数组