NSPredicate(谓词)的一些简单的使用方法

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 首先,我们需要知道何谓谓词,让我们看看官方的解释:The NSPredicate class is used to...
    旭日飞扬阅读 5,427评论 0 0
  • 转载自:http://www.cocoachina.com/ios/20160111/14926.html 1、大...
    一笔春秋阅读 7,828评论 0 2
  • 站在前辈的肩膀上前行 UIKit框架和Foundation框架 所有的Mac OS X和IOS程序都是由大量的对象...
    zysmoon阅读 12,817评论 0 16
  • 八月份非常有成就感的事情之一,是完整学习了《细草老师摄影美学素养》课程。四次讲解,四次作业点评,共八次课程。对于我...
    繁花坞阅读 6,666评论 4 10
  • 祝你岁月无波澜,敬我余生不悲欢。30岁 生日快乐
    VivianKing阅读 1,111评论 0 0