概念:1.用来判别对象是否具有某个特征,对表达式求值返回真、假的过程
2.在oc中使用NSPredicate类来进行判断
比如:
谓词比较运算:
//创建一个对象
Student *student=[Student new];
student.age= 20;
//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);
}