1.創(chuàng)建NSPredicate(謂詞)對(duì)象,謂詞對(duì)象中包含了過濾條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"過濾條件"];
2.判斷指定的對(duì)象是否滿足NSPredicate創(chuàng)建的過濾條件(可以實(shí)際情況可寫可不寫)
[predicate evaluateWithObject:person]; //返回值為BOOL
3. 使用謂詞條件過濾數(shù)組中的元素艾扮,過濾之后返回查詢的結(jié)果
NSArray *persons = [array filteredArrayUsingPredicate:predicate];
接下來有一些簡(jiǎn)單的使用方法
首先是創(chuàng)造數(shù)據(jù)
-(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;
}
根據(jù)年齡篩選出一些人
//找出年齡與第一個(gè)人年齡相同的人
_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]){ //判斷指定的對(duì)象是否滿足
[arrayPredicate addObject:predicatePerson];
}
}
NSLog(@"=====%@",arrayPredicate);
//年齡小于11
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",11];
//使用謂詞條件過濾數(shù)組中的元素穗泵,過濾之后返回查詢的結(jié)果
NSArray *filterArray = [ _allDataArray filteredArrayUsingPredicate:predicate];
NSLog(@"====%@",filterArray);
可以根據(jù)多個(gè)條件同時(shí)滿足篩選出一些人
//查詢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}"];
一些模糊篩選
//name以“孫”開頭的
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH '孫'"];
//name以“精”結(jié)尾的
predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH '精'"];
//name中包含字符“大”的
predicate = [NSPredicate predicateWithFormat:@"name CONTAINS '大'"];
//like 匹配任意多個(gè)字符
//name中只要有“大”字符就滿足條件
predicate = [NSPredicate predicateWithFormat:@"name like '*大*'"];
//?代表一個(gè)字符悍汛,下面的查詢條件是:name中第二個(gè)字符是“大”的
predicate = [NSPredicate predicateWithFormat:@"name like '?大'"];