我需要使用IN
进行过滤。这是我的代码:
let choices = "1,2,3,4"
然后像这样使用它,这会崩溃应用程序:
NSPredicate(format: "id in %@", @choices)
我也尝试过:
NSPredicate(format: "id in [1,2,3,4]")
但是得到这个错误:
'Unable to parse the format string "id in [1,2,3,4]"'
答案 0 :(得分:2)
ng-repeat
- 它是一个字符串,必须是数组<style>
.animation {
-webkit-transition: 1s;
}
.animation.ng-enter {
opacity: 0;
}
.animation.ng-enter.ng-enter-active {
opacity: 1;
}
/* Similar for ng-leave */
</style>
<div class="animation" data-ng-repeat="item in items"> ... </div>
如果let choices = "1,2,3,4"
是您要比较的属性 -
let choices = [1,2,3,4]
将id
替换为NSPredicate(format: "id in %@", @choices)
结果 - @choices
如果要比较对象本身
choices
将NSPredicate(format: "id in %@", choices)
替换为NSPredicate(format: "self in %@", @choices)
,将@choices
替换为choices
结果 - id
self是(指向)对于给定谓词进行比较/评估的对象。
答案 1 :(得分:1)
选项需要用大括号{
和}
包围。然后它会工作。
NSPredicate(format: "id in {\(choices)}")
结果字符串如下所示:
id in {1,2,3,4}