OC之NSPredicate(谓词)学习

时间:2023-01-30 08:45:43

NSPredicate(谓词)

谓词:OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。

实例:

  1. 定义一个person类


    @interface Person : NSObject
    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, assign) int age;
    + (instancetype)personWithName:(NSString *)name andAge:(int)age;
    @end
    //便利初始化方法,直接初始化一个对象
    + (instancetype)personWithName:(NSString *)name andAge:(int)age
    {
    Person *person = [[Person alloc]init];
    person.name = name;
    person.age = age;
    return person;
    }
    - (NSString *)description
    {
    NSString *desc = [NSString stringWithFormat:@"name:%@,age:%d",_name,_age];
    return desc;
    }


  2. 创建一个数组,用来存放person类的对象
     NSArray *personArray = @[
    [Person personWithName:@"niuer" andAge:12],
    [Person personWithName:@"zhangsan" andAge:13],
    [Person personWithName:@"lisi" andAge:14],
    [Person personWithName:@"wangwu" andAge:15],
    [Person personWithName:@"Zhaoliu" andAge:16],
    [Person personWithName:@"tianqi" andAge:17],
    ];



  3. 谓词的基本用法
    1. 关系运算符 > < =
       NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.age > 12 "];
      NSArray *result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);
      predicate = [NSPredicate predicateWithFormat:@"self.age < 15"];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);
      predicate = [NSPredicate predicateWithFormat:@"self.age = 15"];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);

    2. 逻辑运算符 &&(and) ||(或)
      predicate = [NSPredicate predicateWithFormat:@"self.age < 17 && self.age > 12"];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);
      predicate = [NSPredicate predicateWithFormat:@"self.age > 15 || self.name = 'niuer' "];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);

    3. . ANY(至少一个) ALL(所有)
       predicate = [NSPredicate predicateWithFormat:@"ALL self.age < 12"];
      if ([predicate evaluateWithObject:personArray]) {
      NSLog(@"所有人都小于12");
      }else{
      NSLog(@"所有人都大于12");
      }
      predicate = [NSPredicate predicateWithFormat:@"any self.age < 15"];
      if ([predicate evaluateWithObject:personArray]) {
      NSLog(@"至少有一个人小于15");
      }else{
      NSLog(@"所有人都大于15");
      }

    4. 范围运算符 between(闭区间) in(包含)
        predicate = [NSPredicate predicateWithFormat:@"self.age between {13,15}"];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);
      predicate = [NSPredicate predicateWithFormat:@"self.age in{13,15} || self.name in {'zhangsan','lisi'}"];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);

    5. 字符串的操作
       
       下面三个指令一般都加[cd] c:不区分大小写,d  不区分重高音
      beginswit h:以..开始
      endswith :以..结束
      contains :包含...
      predicate = [NSPredicate predicateWithFormat:@"self.name beginswith[cd] 'z' "];
      predicate = [NSPredicate predicateWithFormat:@"self.name beginswith[cd] 'z' "];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);
      predicate = [NSPredicate predicateWithFormat:@"self.name endswith 'i' "];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);
      predicate = [NSPredicate predicateWithFormat:@"self.name contains 'i' "];
      result = [personArray filteredArrayUsingPredicate:predicate];
      NSLog(@"%@",result);