Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Combining Predicates

Is there a way of combining predicates in Swift? For example:

let predicate1 = NSPredicate("self.label = 'foo'"))
let predicate2 = NSPredicate("self.label = 'bar'"))
let combinedPredicate = NSPredicate("self.staticTexts.elementMatchingPredicate(%@).exists AND
  self.staticTexts.elementMatchingPredicate(%@).exists", predicate1, predicate2)

That gives me an error saying predicates cannot be arguments to other predicates. Is there another way to combine them?

like image 827
SalmonKiller Avatar asked Jul 29 '15 18:07

SalmonKiller


1 Answers

You'll need NSCompoundPredicate:

let predicate1 = NSPredicate(format: "self.label = 'foo'", argumentArray: [])
let predicate2 = NSPredicate(format: "self.label = 'bar'", argumentArray: [])
let compound = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate1, predicate2])
like image 193
Glorfindel Avatar answered Oct 30 '22 21:10

Glorfindel