Swift 谓词 AND 和 OR 合而为一

Swift Predicate AND and OR in one

我想做的是将两个 or 查询与一个 and 查询组合起来

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

到目前为止,我已经可以使用 or 和 and 之一,但我不知道如何同时使用两者

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

你需要两个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])