我正在尝试使用 predicateWithSubstitutionVariables 过滤对象数组,此代码引发异常
NSPredicate *pSample = [NSPredicate predicateWithFormat:@"name CONTAINS [c] $variable"];
[pSample predicateWithSubstitutionVariables:@{@"variable":@"sample string"}];
NSLog(@"%@", [mArray filteredArrayUsingPredicate:pSample]);
Run Code Online (Sandbox Code Playgroud)
我得到的例外:
reason: 'Can't get value for 'variable' in bindings {
}.
Run Code Online (Sandbox Code Playgroud)
为什么我不能用这个?(我不是在寻找替代解决方案)
更改您的代码,例如,
NSPredicate *pSample = [NSPredicate predicateWithFormat:@"name CONTAINS [c] $variable"];
NSPredicate *actualPredicate = [pSample predicateWithSubstitutionVariables:@{@"variable":@"sample string"}];
NSLog(@"%@", [mArray filteredArrayUsingPredicate: actualPredicate]);
Run Code Online (Sandbox Code Playgroud)
您创建的第一个谓词pSample是用于创建实际谓词的模板。predicateWithSubstitutionVariables给出你需要的实际谓词。
| 归档时间: |
|
| 查看次数: |
859 次 |
| 最近记录: |