2017-09-24 66 views
2

我想在swift 3中創建一個複雜的NSCompoundPredicate,但是,我不知道該怎麼做。在swift 3中創建複雜的NSCompoundPredicate 3

假設我有5個謂詞(p1,p2,p3,p4,p5)。我要實現以下條件:

compound1 = (p1 AND p2 AND p3) // NSCompoundPredicate(type: .and, 
           //subpredicates: predicates) 
compound2 = (p4 AND p5) // NSCompoundPredicate(type: .and, 
         //subpredicates: predicates) 
compound3 = (compound1 OR compound2) // problem is here 

fetchRequest.predicate = compound3 

NSCompoundPredicate,因爲它的第二個參數得到NSPredicates的陣列,它不渴望。什麼是最好的解決方案?從NSPredicate

回答

4

NSCompoundPredicate繼承,因此你 可以傳遞化合物謂詞創建在所述第一步驟 作爲subpredicate到另一種化合物謂詞:

let compound1 = NSCompoundPredicate(type: .and, subpredicates: [p1, p2, p3]) 
let compound2 = NSCompoundPredicate(type: .and, subpredicates: [p4, p5]) 
let compound3 = NSCompoundPredicate(type: .or, subpredicates: [compound1, compound2]) 

fetchRequest.predicate = compound3