NSPredicate
官方定义:A definition of logical conditions for constraining a search for a fetch or for in-memory filtering.
释义:一种逻辑条件的定义,用于约束检索或内存过滤的搜索。就是用于开发者自定义的条件用来检索过滤数据集合中或对象检索的数据,让你的数据过滤不再for循环。
下面直接上样例代码来解释NSPredicate的用法
//先来创建一些数据源
Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;
Test *test2 = [[Test alloc]init];test2.name = @"asb";test2.code = @2;
Test *test3 = [[Test alloc]init];test3.name = @"raskj";test3.code = @3;
NSArray *objArray = @[test1,test2,test3];
1. 过滤生成新数据 filteredArrayUsingPredicate:
//过滤出name字段长度大于某值的数据
NSPredicate *pred = [NSPredicate predicateWithFormat:@"name.length > %@",@3];
NSArray *filtArray = [objArray filteredArrayUsingPredicate:pred];
2. Array 自身过滤 filterUsingPredicate:
//过滤出name字段长度大于某值的数据,跟filteredArrayUsingPredicate区别的是filterUsingPredicate过滤后自身数据源被过滤掉了
NSMutableArray *objArray = [NSMutableArray arrayWithArray:@[test1,test2,test3]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"name.length > %@",@3];
[objArray filterUsingPredicate:pred];
————————————————————————
3.自身属性做为过滤条件
//字符串数据源对象自身可以参与过滤
NSArray Testarray = @[@"oc", @"swift", @"java", @"python"];
NSPredicate pre = [NSPredicate predicateWithFormat:@"length > 4"];
NSArray *filtArray = [Testarray filteredArrayUsingPredicate:pred];
#过滤结果为 @[python];
4.对象字段属性做为过滤条件
Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;
Test *test2 = [[Test alloc]init];test2.name = @"asb";test2.code = @2;
Test *test3 = [[Test alloc]init];test3.name = @"raskj";test3.code = @3;
NSArray *objArray = @[test1,test2,test3];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"name.length > %@",@3];
NSArray *filtArray = [objArray filteredArrayUsingPredicate:pred];
#过滤结果为 @[test1,test3];
5.字符串相关
CONTAINS(包含)
//使用CONTAINS语句过滤包含的内容
NSArray stringArray = [[NSArray alloc]initWithObjects:@"zizhi",@"chezi",@"reiki", nil];
NSString containStr = @"zi";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", containStr];
NSArray *filtArray = [stringArray filteredArrayUsingPredicate: pre])
6. SELF CONTAINS
表示自身包含 SELF前面加NOT表示相反,切记是放在SELF前不是SELF后
7.[cd]的用法
/*语法可以拼接,拼接时[cd]做为占位符,其中[c] 表示忽略大小写,[d] 表示忽略重音符号[cd]既不区分
大小写,也不区分发音符号。*/
NSArray testArray = [[NSArray alloc]initWithObjects:test1,test2,test3, nil];
NSString targetString = @"Ang";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@",targetString];
NSArray *filtArray = [testArray filteredArrayUsingPredicate:pred]);
8.CONTAINS 包含过滤
NSArray stringArray = [[NSArray alloc]initWithObjects:test1,test2,test3, nil];
NSString targetString = @"ang";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"title CONTAINS %@",targetString];
NSArray *filtArray =[stringArray filteredArrayUsingPredicate:pred]);
BEGINSWITH(已某个字符串开头, begins with)
NSString targetString = @"ang";
NSPredicate pred = [NSPredicate predicateWithFormat:@"title BEGINSWITH %@",targetString];
ENDSWITH(已某个字符串结尾, ends with)
NSString targetString = @"ang";
NSPredicate pred = [NSPredicate predicateWithFormat:@"title ENDSWITH %@",targetString];
9.比较运算符
== 等于
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID == %@",@12];
!= 不等于
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID != %@",@1];
> 大于
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID > %@",@12];
< 小于
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID < %@",@1];
10.范围运算符
IN 之中
NSString
类型,判断title属性是否是字符串@"angle"和@"addss"中的一个:NSPredicate *pred = [NSPredicate predicateWithFormat:@"title IN {'angle', 'addss'}"];
IN 之中
NSNumber
类型,判断testID属性是否是NSNumber对象中的@1和@13中的一个:NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID IN {1, 13}"]
11.BETWEEN 之间
{1, 13}包括1和13:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID BETWEEN {1, 13}"]
12.通配符 LIKE(也可以接[cd])
//包含过滤
NSPredicate pred = [NSPredicate predicateWithFormat:@"title LIKE 'da*'"];
字符串包含ang即 如“danche”、“zidan”
13.? 通配一个字符
//单字符匹配过滤,如“ing”通配 ;“jing”因为是“ji”两个字符就不通配
NSPredicate pred = [NSPredicate predicateWithFormat:@"title LIKE '?ng'"];
14.AND(或&&)
//AND多条件集合,累死sql语句中的and
NSPredicate *pred = [NSPredicate predicateWithFormat:@"testID >= %@ AND testID <=%@", @11, @13];
15.OR(或||)
//OR或语法过滤
NSPredicate *pred = [NSPredicate predicateWithFormat:@"title == 'angle' OR title == 'lenang'"];
16.NOT(或!)
从一个数组中过滤掉另外一个数组的所有数据
//NOT非语法过滤
NSArray arrayFilter = @[@"abc1", @"abc2"];
NSArray arrayContent = @[@"a1", @"abc1", @"abc4", @"abc2"];
NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"NOT (SELF in %@)", arrayFilter];
NSLog(@"%@",[arrayContent filteredArrayUsingPredicate:thePredicate]);
17. 为对象指定字段过滤
NSPredicate *pred = [NSPredicate predicateWithFormat:@" NOT (testID IN %@ )",@[@1, @2]];
18.匹配检测
//对象匹配检测
-(BOOL)evaluateWithObject:(id)object;
Block
Test *test1 = [[Test alloc]init];
test1.name = @"absr";
test1.code = @1;
NSPredicate *pres = [NSPredicate predicateWithFormat:@"code == %@", @2];
BOOL match = [pres evaluateWithObject:test1];
19.block匹配
-(NSPredicate)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary bindings))block
NSArray array = @[@"jim", @"cook", @"jbos"];
NSPredicate pre = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary bindings) {
return [[evaluatedObject valueForKey:@"name"] isEqualToString:@"cook"];
}];
NSArray aray = [array filteredArrayUsingPredicate:pre];
block中第二个参数NSDictionary bindings我看好多文章都写不知道是干嘛的,这里就简单讲一下代表其实就是需要筛选的数组对象的其中一个成员,第二个参数是传过来的参数,如下代码中的@{@"$VALUES":@"name"},可以打个断点观察一下。
NSPredicate* predicateBlock = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
NSLog(@"%@",bindings);
return YES;
}];
[predicateBlock evaluateWithObject:nil substitutionVariables:@{@"$VALUES":@"name"}];
20.组合过滤
//一个或一组NSPredicate形成新的过滤条件
Test *test1 = [[Test alloc]init];test1.name = @"absr";test1.code = @1;
Test *test2 = [[Test alloc]init];test2.name = @"asb";test2.code = @2;
Test *test3 = [[Test alloc]init];test3.name = @"raskj";test3.code = @3;
NSArray *array = @[test1,test2,test3];
NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"code >= %@",@1];
NSPredicate *pre2 = [NSPredicate predicateWithFormat:@"code <= %@",@2];
//以AND形式组合
NSPredicate *andPre = [NSCompoundPredicate andPredicateWithSubpredicates:@[pre1,pre2]];
//以OR形式组合
NSPredicate *orPre = [NSCompoundPredicate orPredicateWithSubpredicates:@[pre1, pre2]];
//过滤条件以外的数据
NSPredicate *notPre = [NSCompoundPredicate notPredicateWithSubpredicate:pre2];
NSArray *filtArray = [array filteredArrayUsingPredicate:orPre];
关于 NSPredicate的用法还有好多好多,大家可以自己慢慢实践