NSPredicate:将CONTAINS与IN组合 [英] NSPredicate: Combine CONTAINS with IN

查看:201
本文介绍了NSPredicate:将CONTAINS与IN组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CoreData中有一组用户,在我的应用程序中有一个搜索字段。用户拥有属性名和名称。

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组合的谓词? / p>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆