NSPredicate:结合 CONTAINS 和 IN [英] NSPredicate: Combine CONTAINS with IN
问题描述
我在 CoreData 中有一组用户,在我的应用程序中有一个搜索字段.用户具有属性 firstname 和 name.
I have a set of users in CoreData and an search field in my application. User has the properties firstname and name.
目前我有一个谓词,如user.name CONTAINS[c] %@ OR user.firstname CONTAINS[c] %@"
Currently I have a predicate like "user.name CONTAINS[c] %@ OR user.firstname CONTAINS[c] %@ "
这一直有效,直到用户键入一个全名,如john smith".即使他输入john sm",也应该能找到 John Smith-Object.
This works until the user types a full name like "john smith". Even if he types "john sm" the John Smith-Object should be found.
将搜索词数组 (IN) 与 CONTAINS 组合在一起的谓词是什么?
What is the predicate to combine an array (IN) of search terms with CONTAINS?
推荐答案
2.5 年后我可以用更复杂的 swift 示例回答我的问题:
2.5 years later I can answer my question with a bit more complex example in swift:
var predicateList = [NSPredicate]()
let words = filterText.componentsSeparatedByString(" ")
for word in words{
if count(word)==0{
continue
}
let firstNamePredicate = NSPredicate(format: "firstName contains[c] %@", word)
let lastNamePredicate = NSPredicate(format: "lastName contains[c] %@", word)
let departmentPredicate = NSPredicate(format: "department contains[c] %@", word)
let jobTitlePredicate = NSPredicate(format: "jobTitle contains[c] %@", word)
let orCompoundPredicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [firstNamePredicate, lastNamePredicate,departmentPredicate,jobTitlePredicate])
predicateList.append(orCompoundPredicate)
}
request.predicate = NSCompoundPredicate(type: NSCompoundPredicateType.AndPredicateType, subpredicates: predicateList)
这篇关于NSPredicate:结合 CONTAINS 和 IN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!