基本用法
比较运算符:
=、==(在谓词中=和==是相同的意思,都是判断)
;<=
;>=
;<
;>;
!=;BETWEEN
:BETWEEN
表达式必须满足表达式 BETWEEN {下限,上限}
的格式,要求该表达式必须大于或等于下限,并小于或等于上限;
例子:
NSNumber *testNumber = @123;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN {100, 200}"];//SELF = 123
等等
if ([predicate evaluateWithObject:testNumber]) {//谓词的方法
NSLog(@"testString:%@", testNumber);
} else {
NSLog(@"不符合条件");
}
逻辑运算符
AND、&&
;OR、||
;NOT、 !
例子:
NSArray *testArray = @[@1, @2, @3, @4, @5, @6];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF > 2 && SELF < 5"];
NSArray *filterArray = [testArray filteredArrayUsingPredicate:predicate];//数组的方法
NSLog(@"filterArray:%@", filterArray);
字符串比较运算符
BEGINSWITH
:检查某个字符串是否以指定的字符串开头(如判断字符串是否以a开头:BEGINSWITH 'a')
ENDSWITH
:检查某个字符串是否以指定的字符串结尾
CONTAINS
:检查某个字符串是否包含指定的字符串
LIKE
:检查某个字符串是否匹配指定的字符串模板。其之后可以跟?
代表一个字符和*
代表任意多个字符两个通配符。比如"name LIKE '*ac*'"
,这表示name的值中包含ac
则返回YES;"name LIKE '?ac*'"
,表示name的第2、3个字符为ac
时返回YES
MATCHES
:检查某个字符串是否匹配指定的正则表达式。虽然正则表达式的执行效率并不高,但其功能是最强大的,也是我们最常用的
注意:
字符串比较都是区分大小写和重音符号的。如:café和cafe是不一样的,Cafe和cafe也是不一样的。如果希望字符串比较运算不区分大小写和重音符号,请在这些运算符后使用[c]
,[d]
选项。其中[c]
是不区分大小写,[d]
是不区分重音符号,其写在字符串比较运算符之后,比如:"name LIKE[cd] 'cafe'"
,那么不论name
是cafe、Cafe还是café上面的表达式都会返回YES
例子:
NSString *string = @"abcdefg";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@" %@ BEGINSWITH 'a'",string];
if ([predicate evaluateWithObject:string]) {
NSLog(@"string:%@", string);
}
集合运算符
ANY、SOME
:集合中任意一个元素满足条件,就返回YES
ALL
:集合中所有元素都满足条件,才返回YES
NONE
:集合中没有任何元素满足条件就返回YES。如:NONE person.age<18
,表示person集合中所有元素的age>=18时,才返回YES
IN
:等价于SQL语句中的IN运算符,只有当左边表达式或值出现在右边的集合中才会返回YES
例子(将array中和filterArray中相同的元素去除):
NSArray *filterArray = @[@"ab", @"abc"];
NSArray *array = @[@"a", @"ab", @"abc", @"abcd"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", filterArray];
NSLog(@"%@", [array filteredArrayUsingPredicate:predicate]);
输出:
基本使用
例子(实例属性值的比较或匹配):
PersonModel *sunnyzl = [PersonModel personWithName:@"sunnyzl" age:29 sex:PersonSexMale];
PersonModel *jack = [PersonModel personWithName:@"jack" age:22 sex:PersonSexMale];
// 首先我们来看一些简单的使用
// 1.判断姓名是否是以s开头的,上面已经用过BEGINSWITH方法,这里用LIKE
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"name LIKE 's*'"];
NSLog(@"sunnyzl:%d, jack:%d", [pred1 evaluateWithObject:sunnyzl], [pred1 evaluateWithObject:jack]);
// 输出为:sunnyzl:1, jack:0
// 2.判断年龄是否大于25
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"age > 25"];
NSLog(@"sunnyzl的年龄是否大于25:%d, jack的年龄是否大于25:%d", [pred2 evaluateWithObject:sunnyzl], [pred2 evaluateWithObject:jack]);
// 输出为:sunnyzl的年龄是否大于25:1, jack的年龄是否大于25:0
例子(
判断手机号是否正确):
- (BOOL)checkPhoneNumber:(NSString *)phoneNumber
{
NSString *regex = @"^[1][3-8]\\d{9}$";//正则表达式
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [pred evaluateWithObject:phoneNumber];
}
iOS中常用的正则表达式
验证用户名和密码---->
^[a-zA-Z]\w{5,15}$
验证电话号码:例:021-68686868 0511-6868686--->^(\\d{3,4}-)\\d{7,8}$
验证手机号码----->^1[3|4|5|7|8][0-9]\\d{8}$
验证身份证号(15位或18位数字)----->\\d{14}[[0-9],0-9xX]
验证Email地址------->^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\.\\w+([-.]\\w+)*$
只能输入由数字和26个英文字母组成的字符串------>^[A-Za-z0-9]+$
整数或者小数------->^[0-9]+([.]{0,1}[0-9]+){0,1}$
只能输入数字------->^[0-9]+$
只能输入n位的数字---->^\\d{n}$
还有很多其他正则表达式请参考:http://www.jianshu.com/p/4b2bad4ad3e8
使用谓词过滤集合
NSArray提供了如下方法使用谓词来过滤集合
- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate:
使用指定的谓词过滤NSArray集合,返回符合条件的元素组成的新集合
NSMutableArray提供了如下方法使用谓词来过滤集合
- (void)filterUsingPredicate:(NSPredicate *)predicate:
使用指定的谓词过滤NSMutableArray,剔除集合中不符合条件的元素
NSSet提供了如下方法使用谓词来过滤集合
- (NSSet *)filteredSetUsingPredicate:(NSPredicate *)predicate NS_AVAILABLE(10_5, 3_0):
作用同NSArray中的方法
NSMutableSet提供了如下方法使用谓词来过滤集合
- (void)filterUsingPredicate:(NSPredicate *)predicate NS_AVAILABLE(10_5, 3_0):
作用同NSMutableArray中的方法
例子:
NSMutableArray *arrayM = [@[@20, @40, @50, @30, @60, @70] mutableCopy];
// 过滤大于50的值
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF > 50"];
[arrayM filterUsingPredicate:pred1];
NSLog(@"arrayM:%@", arrayM);
输出:
例子:
NSArray *array = @[[ZLPersonModel personWithName:@"Jack" age:20 sex:ZLPersonSexMale],
[ZLPersonModel personWithName:@"Rose" age:22 sex:ZLPersonSexFamale],
[ZLPersonModel personWithName:@"Jackson" age:30 sex:ZLPersonSexMale],
[ZLPersonModel personWithName:@"Johnson" age:35 sex:ZLPersonSexMale]];
// 要求取出包含‘son’的元素
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"name CONTAINS 'son'"];
NSArray *newArray = [array filteredArrayUsingPredicate:pred2];
NSLog(@"%@", newArray);
输出:
(
"[name = Jackson, age = 30, sex = 0]",
"[name = Johnson, age = 35, sex = 0]"
)
在谓词中使用占位符参数
首先如果我们想在谓词表达式中使用变量,那么我们需要了解下列两种占位符:
%K
:用于动态传入属性名
%@
:用于动态设置属性值
其实相当于变量名与变量值,除此之外,还可以在谓词表达式中使用动态改变的属性值,就像环境变量一样
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS $VALUE"];
上述表达式中,$VALUE
是一个可以动态变化的值,它其实最后是在字典中的一个key,所以可以根据你的需要写不同的值,但是必须有$开头,随着程序改变$VALUE
这个谓词表达式的比较条件就可以动态改变
例子:
NSArray *array = @[[PersonModel personWithName:@"Jack" age:20 sex:PersonSexMale],
[PersonModel personWithName:@"Rose" age:22 sex:PersonSexFamale],
[PersonModel personWithName:@"Jackson" age:30 sex:PersonSexMale],
[PersonModel personWithName:@"Johnson" age:35 sex:PersonSexMale]];
// 定义一个property来存放属性名,定义一个value来存放值
NSString *property = @"name";
NSString *value = @"Jack";
// 该谓词的作用是如果元素中property属性含有值value时就取出放入新的数组内,这里是name包含Jack
NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", property, value];
NSArray *newArray = [array filteredArrayUsingPredicate:pred];
NSLog(@"newArray:%@", newArray);
// 创建谓词,属性名改为age,要求这个age包含$VALUE字符串
NSPredicate *predTemp = [NSPredicate predicateWithFormat:@"%K > $VALUE", @"age"];
// 指定$VALUE的值为 25
NSPredicate *pred1 = [predTemp predicateWithSubstitutionVariables:@{@"VALUE" : @25}];
NSArray *newArray1 = [array filteredArrayUsingPredicate:pred1];
NSLog(@"newArray1:%@", newArray1);
// 修改 $VALUE的值为32
NSPredicate *pred2 = [predTemp predicateWithSubstitutionVariables:@{@"VALUE" : @32}];
NSArray *newArray2 = [array filteredArrayUsingPredicate:pred2];
NSLog(@"newArray2:%@", newArray2);
输出:
(
"[name = Jack, age = 20, sex = 0]",
"[name = Jackson, age = 30, sex = 0]"
)
(
"[name = Jackson, age = 30, sex = 0]",
"[name = Johnson, age = 35, sex = 0]"
)
(
"[name = Johnson, age = 35, sex = 0]"
)