3.基本语法
(1)比较运算符>,<,==,>=,<=,!=
可用于数值及字符串
(2)范围运算符:IN、BETWEEN
例:@"number BETWEEN {1,5}"
@"address IN {'shanghai','beijing'}"
(3)字符串本身:SELF
例:@“SELF == ‘APPLE’"
(4)字符串相关:BEGINSWITH、ENDSWITH、CONTAINS
例:@"name CONTAIN[cd] 'ang'" //包含某个字符串
@"name BEGINSWITH[c] 'sh'" //以某个字符串开头
@"name ENDSWITH[d] 'ang'" //以某个字符串结束
注:[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。
(5)通配符:LIKE
例:@"name LIKE[cd] '*er*'" //*代表通配符,Like也接受[cd].
@"name LIKE[cd] '???er*'"
1、数组交集
NSPredicate *predicateA = [NSPredicate predicateWithFormat:@"SELF in %@",arrayB];
NSArray *resultArray = [arrayA filteredArrayUsingPredicate:predicateA];
==筛选用法
利用成员实例方法
筛选出长度大于3的字符串
NSArray *array = @[@"jim", @"cook", @"jobs", @"sdevm"];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"length > 3"];
NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);
打印
(
cook,
jobs,
sdevm
)
lenght就是对数组成员执行[xxxx lenght]然后判断返回的NSUInteger值是否大于3。扩展到NSString其他方法比如integerValue
NSArray *array = @[@"2", @"3", @"4", @"5"];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"integerValue >= %@", @3];
NSLog(@"%@", [array filteredArrayUsingPredicate:pre]);
如果我不想用任何实例方法,想筛选成员本身应该怎么做呢。这时候就可以用self来代替
NSPredicate *pre = [NSPredicate predicateWithFormat:@"self CONTAINS %@", @3];