Predicates wrap some combination of expressions and operators and when evaluated return a BOOL.
The NSPredicate class is used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.
// 大概是说: Predicate根据组合表达式的计算结果返回一个BOOL值. 可用于约束搜索或内存过滤.
Creating a Predicate:
+ (NSPredicate *)predicateWithFormat:(NSString *)format, ...
format:
Predicate Format String Syntax:
PS: apple 称这个 format 为文本解释器(predicate string parser), 和正则表达式完全不是一个东西.
空格和关键字大小写不敏感, 支持括号嵌套的表达式, 不做类型检查;
$variable 表示变量, ?是非法关键字;
支持 printf 网络的格式说明符, 其中%k和%@很重要.
%@ 对象值占位符, 一般是一个字符串, 数字, 日期等.
%K key path占位符,(key path 是什么? 看看KVC和KVO, 访问对象属性的技术.)
用%@指定字符串变量时, 它会被解释为带双引号的字符串, 用%K指定一个可变的属性
NSString *attributeName = @"firstName";
NSString *attributeValue = @"Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",
attributeName, attributeValue];
解析结果为: firstName like “Adam” .
单/双引号的变量(或替代变量的字符串)%@,%K,或$变量被解释为一个文本格式字符串,从而防止任何替换。在下面的例子中,解析结果为: firstName like “%@” (注意%@是单引号的)。
NSString *attributeName = @"firstName";
NSString *attributeValue = @"Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like '%@'",
attributeName, attributeValue];
注意: 不能用一个%@代替整个 predicate format 表达式(entire predicate), 如下面的例子不能通过编译:
NSString *expression = @"firstName like Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@", expression];
比较运算(Basic Comparisons)
=, ==
!=,<>
>
<
>=,=>
<=,=<
BETWEEN
以下例子的表达式相当于 ( betweenPredicate >=1 && betweenPredicate<= 10 )
NSPredicate *betweenPredicate =
[NSPredicate predicateWithFormat: @"attributeName BETWEEN %@", @[@1, @10]];
NSDictionary *dictionary = @{ @"attributeName" : @5 };
BOOL between = [betweenPredicate evaluateWithObject:dictionary];
if (between) {
NSLog(@"between");
}
布尔predicate
TRUEPREDICATE 解析为 true
FALSEPREDICATE 解析为 false
NSArray *numberArray = @[@1, @4, @5, @20];
NSPredicate *p;
p = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
NSLog(@"All numbers: %@", [numberArray filteredArrayUsingPredicate:p]);
p = [NSPredicate predicateWithFormat:@"FALSEPREDICATE"];
NSLog(@"No numbers: %@", [numberArray filteredArrayUsingPredicate:p]);
/* printf:结果:
All numbers: (
1,
4,
5,
20
)
No numbers: (
)
*/
逻辑运算(Basic Compound Predicates)
与: AND, &&
或: OR, ||
非: NOT, !
NSPredicate *p = [NSPredicate predicateWithFormat:@"(age > 25) AND (name == %@), @"john"];
字符串计算(String Comparisons)
BEGINSWITH 开始
ENDSWITH 结尾
CONTAINS 包含
LIKE 模糊匹配,可用*和?通配符
MATCHES 正则表达式
以下2个和UTI(universal type identifier)有关,我不知道是什么用途,知道的网友告知我一下。
UTI-CONFORMS-TO
UTI-EQUALS
集合运算(Aggregate Operations)
ANY, SOME 匹配容器中至少有1个元素
ALL 匹配容器中所有元素
NONE 和ALL相反,容器中所有元素都不匹配。
注:以上3个为模糊匹配,IN为精确匹配。
IN 左边的表达式必须出现在右边的集合中,如:name IN { ‘Ben’, ‘Melissa’, ‘Nick’ }
以下4个数组运算不知道怎么使用:
array[index] 数组中特定元素
array[FIRST] 数组的第一个元素
array[LAST] 数组的最后一个元素
array[SIZE] 数组大小
+ predicateWithFormat:
//+ (NSPredicate *)predicateWithFormat:(NSString *)format, ...
NSArray *words = @[
@[@"aab", @"ccd"],
@[@"abc", @"def", @"g"],
@[@"test", @"t2"]
];
NSArray *words2 = @[@"aa", @"bb", @"cddd"];
###+ predicateWithFormat:###
//========筛选元素中包含特定字符串的数组?===========//
//模糊匹配
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"ANY SELF CONTAINS %@", @"aa"];
NSLog(@"array1 = %@",[words filteredArrayUsingPredicate:predicate]);
//精确匹配
predicate = [NSPredicate predicateWithFormat:@"ANY SELF IN %@", @"aab"];
NSLog(@"array2 = %@",[words filteredArrayUsingPredicate:predicate]);
//多个匹配
predicate = [NSPredicate predicateWithFormat:@"ALL SELF IN %@", @[@"test", @"t2"]];
NSLog(@"array3 = %@",[words filteredArrayUsingPredicate:predicate]);
//不包含某字符的匹配
predicate = [NSPredicate predicateWithFormat:@"NONE SELF CONTAINS %@", @"aa"];
NSLog(@"array4 = %@",[words filteredArrayUsingPredicate:predicate]);
//断言(嵌套数组是精确匹配)
predicate = [NSPredicate predicateWithFormat:@"ANY %@ CONTAINS[cd] %@", words,@"a" ];
BOOL exsitW = [predicate evaluateWithObject:words];
NSLog(@"exsitW1 = %@",exsitW ? @"yes": @"no");
//断言(单个数组是模糊匹配)
predicate = [NSPredicate predicateWithFormat:@"ANY %@ CONTAINS %@", words[0],@"a" ];
exsitW = [predicate evaluateWithObject:words];
NSLog(@"exsitW2 = %@",exsitW ? @"yes": @"no");
结果:
2015-08-29 17:10:07.226 GVRegularExpressionDemo[9485:426418] array1 = (
(
aab,
ccd
)
)
2015-08-29 17:10:07.226 GVRegularExpressionDemo[9485:426418] array2 = (
(
aab,
ccd
)
)
2015-08-29 17:10:07.226 GVRegularExpressionDemo[9485:426418] array3 = (
(
test,
t2
)
)
2015-08-29 17:10:07.226 GVRegularExpressionDemo[9485:426418] array4 = (
(
abc,
def,
g
),
(
test,
t2
),
(
h,
i
)
)
2015-08-29 17:10:07.227 GVRegularExpressionDemo[9485:426418] exsitW1 = no
2015-08-29 17:10:07.227 GVRegularExpressionDemo[9485:426418] exsitW2 = yes
+ predicateWithFormat:argumentArray:
// + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat
// argumentArray:(NSArray *)arguments
NSArray *argArr = @[ @[@"h",@"i"] ];
predicate = [NSPredicate predicateWithFormat:@"ALL SELF IN %@" argumentArray:argArr];
NSLog(@"array5 = %@",[words filteredArrayUsingPredicate:predicate]);
/* printf:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] array5 = (
(
h,
i
)
)
*/
+predicateWithFormat:arguments:
//+ (NSPredicate *)predicateWithFormat:(NSString *)format
// arguments:(va_list)argList
-(void)arg_list_test:(NSArray*)words andArgList:(NSString*)args, ...{
va_list argList;
va_start(argList, args);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF CONTAINS %@" arguments:argList];
va_end(argList);
NSLog(@"array6 = %@",[words filteredArrayUsingPredicate:predicate]);
}
[self arg_list_test:words andArgList:@"a", @"c"];
/* printf:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] array6 = (
(
aab,
ccd
),
(
abc,
def,
g
)
)
*/
- predicateWithSubstitutionVariables:
// - (instancetype)predicateWithSubstitutionVariables:(NSDictionary *)variables
predicate = [NSPredicate predicateWithFormat:@"ANY SELF IN $strVar"];
NSPredicate *subPre = [predicate predicateWithSubstitutionVariables:@{@"strVar":@"aab"}];
NSLog(@"array7 = %@",[words filteredArrayUsingPredicate:subPre]);
/* printf:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] array7 = (
(
aab,
ccd
)
)
*/
+ predicateWithValue:
// + (NSPredicate *)predicateWithValue:(BOOL)value
predicate = [NSPredicate predicateWithValue:YES];
NSLog(@"array8 = %@",[words filteredArrayUsingPredicate:predicate]);
/* printf:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] array8 = (
(
aab,
ccd
),
(
abc,
def,
g
),
(
test,
t2
),
(
h,
i
)
)
*/
+ predicateWithBlock:
//+ (NSPredicate *)predicateWithBlock:(BOOL (^)(id evaluatedObject,
// NSDictionary *bindings))block
predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSLog(@"evaluatedObject = %@, bindings = %@",evaluatedObject, bindings);
if (bindings) {
return [[evaluatedObject objectAtIndex:0] isEqualToString:[bindings valueForKey:@"value"]];
}else{
return [evaluatedObject isEqualToString:@"aa"];
}
}];
NSLog(@"exsitW3 = %d", [predicate evaluateWithObject:words2 substitutionVariables:@{@"value":@"aa"}]);
NSLog(@"array9 = %@", [words2 filteredArrayUsingPredicate:predicate]);
结果:
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] evaluatedObject = (
aa,
bb,
cddd
), bindings = {
value = aa;
}
2015-08-29 17:10:07.247 GVRegularExpressionDemo[9485:426418] exsitW3 = 1
2015-08-29 17:10:07.248 GVRegularExpressionDemo[9485:426418] evaluatedObject = aa, bindings = (null)
2015-08-29 17:10:07.248 GVRegularExpressionDemo[9485:426418] evaluatedObject = bb, bindings = (null)
2015-08-29 17:10:07.249 GVRegularExpressionDemo[9485:426418] evaluatedObject = cddd, bindings = (null)
2015-08-29 17:10:07.249 GVRegularExpressionDemo[9485:426418] array9 = (
aa
)
Evaluating a Predicate
- evaluateWithObject:
//- (BOOL)evaluateWithObject:(id)object
//… 上面用得太多, 不写了.
- evaluateWithObject:substitutionVariables:
// - (BOOL)evaluateWithObject:(id)object
// substitutionVariables:(NSDictionary *)variables
predicate = [NSPredicate predicateWithFormat:@"ANY SELF IN $strVar"];
BOOL exsitW4 = [predicate evaluateWithObject:words2 substitutionVariables:@{@"strVar":@"aaaaaaaaaaaaaaaaa"}];
NSLog(@"exsitW4 = %@", exsitW4 ? @"yes" : @"no");
/* printf:
exsitW4 = yes
*/
- allowEvaluation
// - (void)allowEvaluation //不明白有什么用途 ?????, 知道的盆友告诉我一下.
[predicate allowEvaluation]; //不明白有什么用途
Getting a String Representation
predicateFormat Property
// @property(readonly, copy) NSString *predicateFormat
NSLog(@"predicate.predicateFormat = %@",predicate.predicateFormat);
/* printf:
predicate.predicateFormat = ANY SELF IN $strVar
*/