谓词:NSPredicate用法的简单介绍

谓词:NSPredicate

概念:1.用来判别对象是否具有某个特征,对表达式求值返回真、假的过程

           2.在oc中使用NSPredicate类来进行判断

比如:

谓词比较运算:

//创建一个对象

    Student *student=[Student new];

    student.age20;

    //NSPredicate-谓词

    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"age=<19"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"age=<19");

    } else {

        NSLog(@"age>19");

    }

 //in

    predicate=[NSPredicate predicateWithFormat:@"age in{20,30}"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"在里面");

    } else {

        NSLog(@"不在里面");

    }

    //between 包括大于等于&&小于等于

    predicate=[NSPredicate predicateWithFormat:@"age BETWEEN{20,30}"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"在这个范围里");

    } else {

        NSLog(@"不在这个范围里");

    }

谓词字符串匹配用法:

//BEGINSWITH,CONTAINS,ENDSWITH,LIKE,MATCHES

    student.name=@"yongge";

    predicate=[NSPredicate predicateWithFormat:@"name BEGINSWITH'yong'"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"yong开头");

    } else {

         NSLog(@"不以yong开头");

    }

    predicate=[NSPredicate predicateWithFormat:@"name CONTAINS'yong'"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"包含yong");

    } else {

        NSLog(@"不包含yong开头");

    }

    predicate=[NSPredicate predicateWithFormat:@"name ENDSWITH'ge'"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"ge结尾");

    } else {

        NSLog(@“不ge结尾");

    }

谓词的查找用法:

LIKE简单匹配:

 //[c]不区分大小写

    //替换Keypath,用大写%K

    //%@是占位符

    predicate=[NSPredicate predicateWithFormat:@"%K LIKE[c]%@",@"name",@"yong*"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"找到yong");

    } else {

        NSLog(@"没找到");

    }

MATCHES正规匹配:

    predicate=[NSPredicate predicateWithFormat:@"name MATCHES'yong?.*'"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"ge结尾");

    } else {

        NSLog(@"ge结尾");

    }

谓词聚合匹配:

//谓词聚合运算(ANY//SOME ALL NONE)用在集合类(NSArray,NSSet)

    student.books=@[@"水浒传",@"三国演义"];

    predicate=[NSPredicate predicateWithFormat:@"ANY books LIKE[c] '*'"];

    if ([predicate evaluateWithObject:student]) {

        NSLog(@"ge结尾");

    } else {

        NSLog(@"ge结尾");

    }

谓词的应用:

谓词用于定义一个逻辑条件,通过该条件可以执行搜索或对内存中的数据过滤;

数组和集合提供了使用谓词进行过滤的方法

//用谓词对集合的数据进行操作

Student *stu1=[[Student alloc]initName:@"lisi" andAge:22 andBooks:@[@"test1",@"test2"]];

    Student *stu2=[[Student alloc]initName:@"zhangshan" andAge:20 andBooks:@[@"test1",@"test2",@"book1"]];

    Student *stu3=[[Student alloc]initName:@"lisan" andAge:24 andBooks:@[@"test1",@"test2"]];

    Student *stu4=[[Student alloc]initName:@"wangba" andAge:19 andBooks:@[@"test1",@"book2",@"test2"]];

    NSArray *arr=@[stu1,stu2,stu3,stu4];

    predicate=[NSPredicate predicateWithFormat:@"age>=20"];

    NSArray *arr1=[arr filteredArrayUsingPredicate:predicate];

    for (Student*stu in arr1) {

        NSLog(@"%@,%ld",stu.name,stu.age);

    }

    predicate=[NSPredicate predicateWithFormat:@"SOME books LIKE 'book*'"];

    NSLog(@"%@",predicate.predicateFormat);

    arr1=[arr filteredArrayUsingPredicate:predicate];

    for (Student*stu in arr1) {

        NSLog(@"%@,%ld",stu.name,stu.age);

    }

    predicate=[NSPredicate predicateWithFormat:@"name LIKE 'li*'"];

    NSLog(@"%@",predicate.predicateFormat);

    arr1=[arr filteredArrayUsingPredicate:predicate];

    for (Student*stu in arr1) {

        NSLog(@"%@,%ld",stu.name,stu.age);

    }

//用谓词对数组的数据进行过滤

    NSArray *strArr=@[@12,@13,@57,@34];

    predicate=[NSPredicate predicateWithFormat:@"SELF>13"];

    NSLog(@"%@",predicate.predicateFormat);

    NSArray  *arr2=[strArr filteredArrayUsingPredicate:predicate];

    for ( NSString* str  in arr2) {

        NSLog(@"%@",str);

    }

    predicate=[NSPredicate predicateWithFormat:@"self<57"];

    NSArray *arr3=[strArr filteredArrayUsingPredicate:predicate];

    for (NSString *str1 in arr3) {

        NSLog(@"%@",str1);

    }

转载于:https://my.oschina.net/u/2529338/blog/549259

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值