我可以使用 `predicateWithSubstitutionVariables` 或类似的东西作为键而不是值吗?
Can I use `predicateWithSubstitutionVariables` or something similar for the key instead of the value?
有一次我询问如何将 1 个关键字传递给多个键并得到这个答案:. And then, I asked for how to pass the key as param: Pass the key as param in NSPredicate
所以现在,我很好奇我是否可以反之亦然:我可以将多个键作为参数传递给 1 个(或许多)搜索词吗?如果是,那会是什么样子?
一些代码供你们编辑。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(($key = 'value1')
@" OR ($key = 'value2')) "
@" AND ($key != '0') "];
predicate = [predicate predicateWithSubstitutionVariables:@{@"key": @"myProperty"}];
请注意,在谓词中,有一个 (AND !=) 所以我将无法使用 IN predicate
:
[NSPredicate predicateWithFormat:@"myProperty IN %@",
@[@"value1", @"value2", @"value3"]];
这些只是一些示例代码,所以请不要尝试修复谓词,而是关注问题:substituteVariables
变量是过滤的关键。
键必须是键路径表达式:
[predicate predicateWithSubstitutionVariables:@{@"key": [NSExpression expressionForKeyPath:@"myProperty"]}]
将导致谓词
(myProperty == "value1" OR myProperty == "value2") AND myProperty != "0"
有一次我询问如何将 1 个关键字传递给多个键并得到这个答案:
所以现在,我很好奇我是否可以反之亦然:我可以将多个键作为参数传递给 1 个(或许多)搜索词吗?如果是,那会是什么样子?
一些代码供你们编辑。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(($key = 'value1')
@" OR ($key = 'value2')) "
@" AND ($key != '0') "];
predicate = [predicate predicateWithSubstitutionVariables:@{@"key": @"myProperty"}];
请注意,在谓词中,有一个 (AND !=) 所以我将无法使用 IN predicate
:
[NSPredicate predicateWithFormat:@"myProperty IN %@",
@[@"value1", @"value2", @"value3"]];
这些只是一些示例代码,所以请不要尝试修复谓词,而是关注问题:substituteVariables
变量是过滤的关键。
键必须是键路径表达式:
[predicate predicateWithSubstitutionVariables:@{@"key": [NSExpression expressionForKeyPath:@"myProperty"]}]
将导致谓词
(myProperty == "value1" OR myProperty == "value2") AND myProperty != "0"