<pre name="code" class="objc"> //返回一个符合谓词条件的数组
NSArray *newArray = [ array filteredArrayUsingPredicate:predicate];
for (Person *person in newArray) {
//NSLog(@"person name : %@", [person valueForKey:@"_name"]);
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@" age <= 28"];
//表示指定对象是否满足谓词条件
for (Person *person in array) {
if ([predicate evaluateWithObject:person]) {
// NSLog(@"PERSON NAME : %@",person.name);
}
}
//格式占位符
NSPredicate *pre = [NSPredicate predicateWithFormat:@"age <= %d",30];
NSArray *array2 = [array filteredArrayUsingPredicate:pre];
for (Person *person in array2) {
//NSLog(@"person name 2 : %@", [person valueForKey:@"_name"]);
}
//逻辑运算符
//逻辑符号的加入,谓词区分大小 && AND || OR
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"name > 'bruse' && age < %d ",30];
NSArray *array3 = [array filteredArrayUsingPredicate:pre3];
//关键字 注意字符串一定要添加 ' '
NSPredicate *pre4 = [NSPredicate predicateWithFormat:@"self.name IN {'rose','bruse'} "];
NSArray *array4 = [array filteredArrayUsingPredicate:pre4];
for (Person *person in array4) {
//NSLog(@"person name 4 : %@", [array4 valueForKey:@"_name"]);
}
//以**开始 --------BEGINSWITH
//BEGINSWITH 检查某个字是否以**开头
NSPredicate *pre5 = [NSPredicate predicateWithFormat:@"self.name BEGINSWITH 'j' " ];
NSArray *array5 = [array filteredArrayUsingPredicate:pre5];
NSLog(@"person name : %@",[array5 valueForKey:@"name"]);
//以**结束 --------ENDSWITH
//ENDSWITH 检查某个字是否以**结尾
NSPredicate *pre6 = [NSPredicate predicateWithFormat:@"self.name ENDSWITH 'E' " ];
NSArray *array6 = [array filteredArrayUsingPredicate:pre6];
NSLog(@"array6 : %@",[array6 valueForKey:@"name"]);
//包含 --------CONTAINS
//CONTAINS 检查包含某个字符
NSPredicate *pre7 = [NSPredicate predicateWithFormat:@"self.name CONTAINS '小' " ];
NSArray *array7 = [array filteredArrayUsingPredicate:pre7];
NSLog(@"array7 : %@",[array7 valueForKey:@"name"]);
//模糊查询 --------like
NSPredicate *pre8 = [NSPredicate predicateWithFormat:@"name like '*%@*' ",@"a"];
NSArray *array8 = [array filteredArrayUsingPredicate:pre8];
NSLog(@"array8 : %@",[array8 valueForKey:@"name"]);