2017-09-24 18 views
2

複雑なNSCompoundPredicateを迅速に作成したいが、これを行う方法がわからない。複雑な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それは2番目の引数が、それは望んでいないことを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