本文介绍了在Swift 3中创建复杂的NSCompoundPredicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在swift 3中创建一个复杂的 NSCompoundPredicate ,但是,我不知道该怎么做。

I want to create a complicated NSCompoundPredicate in swift 3, however, I don't know how to do this.

假设我有5个谓词(p1,p2,p3,p4,p5)。我想实现以下条件:

Suppose I have 5 predicates (p1,p2,p3,p4,p5). I want to implement below conditions:

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数组。最好的解决方案是什么?

NSCompoundPredicate as it's second argument gets array of NSPredicates that it doesn't desire. What is the best solution?

推荐答案

NSCompoundPredicate 继承自 NSPredicate ,因此您
可以将在第一步中创建的复合谓词
作为子谓词传递给另一个复合谓词:

NSCompoundPredicate inherits from NSPredicate, therefore youcan pass the compound predicates creates in the first stepsas subpredicate to another compound predicate:

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

这篇关于在Swift 3中创建复杂的NSCompoundPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:22