Ceri Turner
Ceri Turner

Reputation: 936

Swift Predicate AND and OR in one

What I'm wanting to do is combine two or queries with an and query

WHERE (id contains[cd] %@ OR name contains[cd] %@) AND manu = %@"

So far I have it working with one of the ors and the and, but I can't figure out how to do both

let predicate1 = NSPredicate(format: "id contains[cd] %@", searchedText)
let predicate2 = NSPredicate(format: "threadManu = %@", manu)
predicate = NSCompoundPredicate.init(type: .and, subpredicates: [predicate1,predicate2])

Upvotes: 0

Views: 1697

Answers (1)

rmaddy
rmaddy

Reputation: 318794

You need two NSCompoundPredicate.

let predicate1 = NSPredicate(format: "id contains[cd] %@", searchedText)
let predicate2 = NSPredicate(format: "name contains[cd] %@", searchedText)
let predicate3 = NSPredicate(format: "threadManu = %@", manu)

let predicateOr = NSCompoundPredicate(type: .or, subpredicates: [predicate1, predicate2])
let predicate = NSCompoundPredicate(type: .and, subpredicates: [predicateOr, predicate3])

Upvotes: 6

Related Questions