1.常用方法
// 1.创建NSPredicate(相当于创建一个过滤条件)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"过滤条件"];
// 2.判断指定的对象是否满足NSPredicate创建的过滤条件
[predicate evaluateWithObject:person];
// 3.过滤出符合条件的对象(返回所有符合条件的对象)
NSArray *persons = [array filteredArrayUsingPredicate:predicate];
NSArray *arrPersons = @[
[[Person alloc] initWithName:@"Mark" gender:@"M" age:36],
[[Person alloc] initWithName:@"Andy" gender:@"F" age:29],
[[Person alloc] initWithName:@"Jack" gender:@"M" age:20],
[[Person alloc] initWithName:@"Jobs" gender:@"M" age:59],
[[Person alloc] initWithName:@"Elizabeth" gender:@"F" age:81],
];
```
2.比较运算符 >,<,==,>=,<=,!=,AND,&&,OR,||
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age>50 AND gender='F'"]; // 注意这个语法里面字符串要用单引号
NSArray *result = [arrPersons filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
输出结果:
(
"Elizabeth-F-81"
)
*/
3.范围运算符,in(则表示在后面的{}中是否包含),between(在某个范围内,如:age between{15,25}等价于 15<=age<=25
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age between{20,30}"];
NSArray *result = [arrPersons filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
输出结果:
(
"Andy-F-29",
"Jack-M-20"
)
*/
//场景3
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age in{20,30}"];
NSArray *result = [arrPersons filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
输出结果:
(
"Jack-M-20"
)
*/
//场景4
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name in{'Jobs','Mark'}"]; //当条件是NSString时 in 和 between 结果一致
NSArray *result = [arrPersons filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
输出结果:
(
"Mark-M-36",
"Jobs-M-59"
)
*/
4.对象本身self 如@"SELF == 'Apple'"(也可以是NSNumber)
NSArray *arr = @[@"Apple",@"Mac",@"iPhone"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF == 'Apple'"];
NSArray *result = [arr filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
/*
输出结果:
(
Apple
)
*/
5.字符串相关
:BEGINSWITH、ENDSWITH、CONTAINS
例:@"name CONTAINS[cd] 'ang'" //包含某个字符串
@"name BEGINSWITH[c] 'sh'" //以某个字符串开头
@"name ENDSWITH[d] 'ang'" //以某个字符串结束
//场景6
NSArray *arr = @[@"yangjie",@"zhangsan",@"lisi"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self contains[cd] 'ng'"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self beginswith[c] 'y'"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self endswith[d] 'i'"];
6.通配符:LIKE
// 例:@"name LIKE[cd] 'er'" //代表通配符,Like也接受[cd].
// @"name LIKE[cd] '???er'"
NSArray *arr = @[@"yangjie",@"zhangsan",@"lisi",@"ang",@"tangddd",@"fangxxx"];
NSPredicate predicate = [NSPredicate predicateWithFormat:@"self like[cd] 'ang'"];
// 注意这个语法里面字符串要用单引号
NSArray *result = [arr filteredArrayUsingPredicate:predicate];
// 打印输出结果
NSLog(@"%@",result);
// 打印输出结果
NSLog(@"%@",result);
/*
输出结果:
(
ang
)
*/
7.正则表达式:MATCHES
NSPredicate 使用MATCHES 匹配正则表达式,正则表达式的写法采用international components
for Unicode (ICU)的正则语法。
例:NSString *regex = @"^A.+e$"; //以A开头,e结尾
@"name MATCHES %@",regex
例:
NSString *regex = @"^A.+e$";//以A 开头,以e 结尾的字符。
NSPredicate *pre= [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if([pre evaluateWithObject: @"Apple"]){
printf("YES\n");
}else{
printf("NO\n");
}