NSPredicate 谓词(过滤器)

来源:互联网 发布:剑三唱歌捏开脸数据 编辑:程序博客网 时间:2024/06/06 20:48
简介:谓词相当于是过滤器,用来判断真假是否的

使用3大步:条 谓 真

//1.谓词条件:筛选以什么开始 [3步]NSString *matchStr = @"self beginswith [c] 'a'";//2.创建谓词NSPredicate *predicate = [NSPredicate predicateWithFormat:matchStr];//3.判断真假BOOL isHas = [predicate evaluateWithObject:Str];NSLog( @"isHas>>>%d",isHas);

其他Demo:

//2.谓词条件:以什么结尾NSString *matchStr2 = @"self endswith [c] 'e'";NSPredicate *predicate2 = [NSPredicate predicateWithFormat:matchStr2];BOOL isHas2 = [predicate2 evaluateWithObject:Str];NSLog( @"isHas2>>>%d",isHas2);//3.判断是不是邮箱NSString *zhenze = @"asdasdasd";NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"self matches %@",zhenze];BOOL isHas3 = [predicate3 evaluateWithObject:Str];NSLog( @"邮箱>>>%d",isHas3);//4.根据谓词条件刷选数组NSArray *array = @[@"asd",@"asd"];NSString *pre4 =@"self beginswith [c] 'a'";NSPredicate *predicate4 = [NSPredicate predicateWithFormat:pre4];array = [array filteredArrayUsingPredicate:predicate4];NSLog(@"%@",array);//5.判断对象的属性 例:名字Peopel *p = [[Peopel alloc]init];p.name = @"zs";//NSString *pre5 = @"name like '*d*'";//名字里面是否包含dNSString *pre5 = @"name like 'z?'";//名字是否以 z 开头NSPredicate *predicate5 = [NSPredicate predicateWithFormat:pre5];BOOL p5= [predicate5 evaluateWithObject:p];NSLog(@"%d",p5);
0 0
原创粉丝点击