我想编写一个 CoreData
谓词来检查一列中有多少记录与输入的任何关键字匹配,而不管它们出现的顺序如何。
例如。假设我有一条记录“This is my test string to compare”,现在如果我搜索“test my compare”它应该返回这条记录。
我已经尝试使用分离单词并为每个单词使用 CONTAINS
和 OR
ed ,但是由于有超过 15000 条记录,结果需要很长时间在数据库中。
请帮忙。
最佳答案
Create contains predicate
用于字符串中的每个单词并使用 AND 类型的 NSCompoundPredicate
。
let string = "test my compare"
var pridicateArray : [NSPredicate]!
for word in string.components(separatedBy: " ") {
let newPredicate = NSPredicate(format: "YOUR_ENTITY contains[c] %@", word)
pridicateArray.append(newPredicate)
}
let andPredicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.and, subpredicates: pridicateArray)
关于ios - Coredata 谓词,用于匹配列中具有任意出现顺序的多个单词的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54324292/