NSPredicate: Combine CONTAINS with IN

后端 未结 4 834
庸人自扰
庸人自扰 2020-11-29 09:42

I have a set of users in CoreData and an search field in my application. User has the properties firstname and name.

Currently I have a predicate like \"user.name CO

4条回答
  •  感情败类
    2020-11-29 10:25

    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)
    

提交回复
热议问题