1.创建NSPredicate(谓词)对象,谓词对象中包含了过滤条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"过滤条件"];
2.判断指定的对象是否满足NSPredicate创建的过滤条件(可以实际情况可写可不写)
[predicate evaluateWithObject:person]; //返回值为BOOL
3. 使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray *persons = [array filteredArrayUsingPredicate:predicate];
接下来有一些简单的使用方法
首先是创造数据
-(NSMutableArray *)setUpDataArray
{
NSDictionary * persondic =[NSDictionary dictionaryWithObjectsAndKeys:@11,@"age",@"猪八戒",@"name", nil];
NSDictionary * persondic1 =[NSDictionary dictionaryWithObjectsAndKeys:@21,@"age",@"猪无能",@"name", nil];
NSDictionary * persondic2 =[NSDictionary dictionaryWithObjectsAndKeys:@11,@"age",@"孙悟空",@"name", nil];
NSDictionary * persondic3 =[NSDictionary dictionaryWithObjectsAndKeys:@11,@"age",@"孙大圣",@"name", nil];
NSDictionary * persondic4 =[NSDictionary dictionaryWithObjectsAndKeys:@13,@"age",@"孙行者",@"name", nil];
NSDictionary * persondic5 =[NSDictionary dictionaryWithObjectsAndKeys:@13,@"age",@"唐僧",@"name", nil];
NSDictionary * persondic6 =[NSDictionary dictionaryWithObjectsAndKeys:@14,@"age",@"沙和尚",@"name", nil];
NSDictionary * persondic7 =[NSDictionary dictionaryWithObjectsAndKeys:@15,@"age",@"白骨精",@"name", nil];
NSDictionary * persondic8 =[NSDictionary dictionaryWithObjectsAndKeys:@11,@"age",@"蜘蛛精",@"name", nil];
NSDictionary * persondic9 =[NSDictionary dictionaryWithObjectsAndKeys:@15,@"age",@"哪吒",@"name", nil];
NSDictionary * persondic10 =[NSDictionary dictionaryWithObjectsAndKeys:@15,@"age",@"王母",@"name", nil];
NSDictionary * persondic11 =[NSDictionary dictionaryWithObjectsAndKeys:@16,@"age",@"玉皇大帝",@"name", nil];
NSArray * personArray =[NSArray arrayWithObjects:persondic,persondic1,persondic2,persondic3,persondic4,persondic5,persondic6,persondic7,persondic8,persondic9,persondic10,persondic11, nil];
NSMutableArray * dataArray=[[ NSMutableArray alloc]init];
for(NSDictionary * dic in personArray){
Person * person = [[Person alloc]init];
person.name =dic[@"name"];
person.age =[dic[@"age"] integerValue];
[dataArray addObject:person];
}
return dataArray;
}
根据年龄筛选出一些人
//找出年龄与第一个人年龄相同的人
_allDataArray =[self setUpDataArray];
Person * model = _allDataArray.firstObject;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age=%ld",model.age];
NSMutableArray * arrayPredicate = [[NSMutableArray alloc]init];
for (int j = 0 ; j < _allDataArray.count ; j ++ ) {
Person * predicatePerson = _allDataArray[j];
if([predicate evaluateWithObject:predicatePerson]){ //判断指定的对象是否满足
[arrayPredicate addObject:predicatePerson];
}
}
NSLog(@"=====%@",arrayPredicate);
//年龄小于11
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",11];
//使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray *filterArray = [ _allDataArray filteredArrayUsingPredicate:predicate];
NSLog(@"====%@",filterArray);
根据年龄筛.png
可以根据多个条件同时满足筛选出一些人
//查询name=孙悟空的并且age等于11
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name='孙悟空' && age=11"];
NSArray *filterArray = [ _allDataArray filteredArrayUsingPredicate:predicate];
NSLog(@"====%@",filterArray);
//IN(包含)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.name IN {'孙行者','唐僧','猪八戒'} || self.age IN{11,13}"];
可以根据多个条件筛选.png
一些模糊筛选
//name以“孙”开头的
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH '孙'"];
//name以“精”结尾的
predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH '精'"];
//name中包含字符“大”的
predicate = [NSPredicate predicateWithFormat:@"name CONTAINS '大'"];
//like 匹配任意多个字符
//name中只要有“大”字符就满足条件
predicate = [NSPredicate predicateWithFormat:@"name like '*大*'"];
//?代表一个字符,下面的查询条件是:name中第二个字符是“大”的
predicate = [NSPredicate predicateWithFormat:@"name like '?大'"];
模糊筛选.png
网友评论