链接替换在NSPredicate中不起作用 [英] Chaining replacements don't work in NSPredicate
问题描述
以下代码提出了一个问题
The following code raises a question
NSPredicate * myPred = [NSPredicate predicateWithFormat:@"$my_var.employees.@count == 3"];
NSExpression * myExpr = [NSExpression expressionForKeyPath:@"$my_var.test"] ;
NSPredicate * myPredBis = [myPred predicateWithSubstitutionVariables:@{@"my_var" : myExpr}] ;
NSExpression * myExpr2 = [NSExpression expressionForKeyPath:@"self"] ;
NSPredicate * myFinalPred = [myPredBis predicateWithSubstitutionVariables:@{@"my_var": myExpr2}];
NSLog(@"%@", myFinalPred) ;
它打印
$my_var.test.employees.@count == 3
但是我们想要
self.test.employees.@count == 3
为什么会这样?
推荐答案
这只是部分答案,但这也许有助于您走上正确的道路.
This is only a partial answer, but perhaps it helps to put you on the right path.
首先,将 $ my_var
替换为 [NSExpression expressionForKeyPath:@"$ my_var.test"]
不起作用,因为后一个表达式将 $ my_var
视为纯键,而不是变量.
First, substituting $my_var
by [NSExpression expressionForKeyPath:@"$my_var.test"]
does not work, because the latter expression treats $my_var
as a plain key, not as
a variable.
但是您可以使用 expressionForVariable
将一个变量替换为另一个变量:
But you can substitute one variable by another variable, using expressionForVariable
:
NSPredicate * myPred = [NSPredicate predicateWithFormat:@"$my_var.employees.@count == 3"];
NSExpression * myExpr = [NSExpression expressionForVariable:@"another_var"] ;
NSPredicate * myPredBis = [myPred predicateWithSubstitutionVariables:@{@"my_var" : myExpr}] ;
第二, [NSExpression expressionForKeyPath:@"self"]
将"self"视为普通键,而不是"the" self ,即对象谓词被应用.你可能想要
Second, [NSExpression expressionForKeyPath:@"self"]
treats "self" as an ordinary key, not as "the" self, i.e. the object onto which the predicate is applied. You probably
want:
NSExpression * myExpr2 = [NSExpression expressionForEvaluatedObject];
相反.现在您可以链接替换了:
instead. Now you can chain the substitution:
NSPredicate * myFinalPred = [myPredBis predicateWithSubstitutionVariables:@{@"another_var": myExpr2}];
NSLog(@"%@", myFinalPred);
输出: employees.@ count == 3
("self"部分只是不打印).
Output: employees.@count == 3
(the "self" part is just not printed).
好的,这是您解决问题的方法.棘手的部分是第二步.我没有找到构建包含以下内容的键路径表达式的直接方法:变量( $ my_var
)与普通键( test
)组合.但这是可以完成的通过获取谓词的左侧:
OK, here is the solution to your problem. The tricky part is the second step.
I did not find a direct way to build a key-path expression that consists of a
variable ($my_var
) combined with a normal key (test
). But it can be done
by taking the left-hand side of a predicate:
NSPredicate * myPred = [NSPredicate predicateWithFormat:@"$my_var.employees.@count == 3"];
NSLog(@"%@", myPred) ;
// $my_var.employees.@count == 3
NSExpression *myExpr = [(NSComparisonPredicate *)[NSPredicate predicateWithFormat:@"$my_var.test == 0"] leftExpression];
NSPredicate * myPredBis = [myPred predicateWithSubstitutionVariables:@{@"my_var" : myExpr}] ;
NSLog(@"%@", myPredBis) ;
// $my_var.test.employees.@count == 3
NSExpression * myExpr2 = [NSExpression expressionForEvaluatedObject];
NSPredicate * myFinalPred = [myPredBis predicateWithSubstitutionVariables:@{@"my_var": myExpr2}];
NSLog(@"%@", myFinalPred) ;
// test.employees.@count == 3
这篇关于链接替换在NSPredicate中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!