【问题标题】:Create complicated NSCompoundPredicate in swift 3在 swift 3 中创建复杂的 NSCompoundPredicate
【发布时间】:2017-09-24 09:02:12
【问题描述】:

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

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

【问题讨论】:

    标签: swift core-data nspredicate nscompoundpredicate


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多