【发布时间】:2021-02-05 20:54:49
【问题描述】:
我想使用 NSPredicate 执行此过滤方法,但在转换语法时遇到问题。
objectsCollection.filter { $0.stringIds.contains(id) }
示例类
class Object {
let stringIds: [String]
}
let objectsCollection: [Object]
let id = "The id we want to look for"
objectsCollection.filter { $0.stringIds.contains(id) }
我对 NSPredicate 的尝试
这是我认为它会工作的方式,但似乎没有。
let filter = NSPredicate(format: "%@ IN stringIds", id)
objectsCollection.filter(filter)
错误
reason: 'Expected object of type (null) for property 'stringIds' on object of type 'Object', but received: 6011ea4dda6853a3af97376e'
【问题讨论】:
-
" 但好像没有。"会发生什么?
-
刚刚用我收到@Alexander 的错误更新了问题
-
在我的原始代码中,我将字符串转换为 NSPredicate(format: filterString) 我只是没有在这个问题中包含它
-
我对领域一无所知,但 Swift 数组
filter方法根本不需要 NSPredicate。但是,如果这是 确实 采用 NSPredicate 的其他filter方法,我会问,你可以在这里使用 block 谓词吗?它们更容易编写。 -
你能举一个块谓词的例子吗?
标签: swift realm nspredicate