【问题标题】:Combining + andPredicateWithSubpredicates: and + orPredicateWithSubpredicates: in one predicate结合 + andPredicateWithSubpredicates: 和 + orPredicateWithSubpredicates: 在一个谓词中
【发布时间】:2013-09-01 21:53:22
【问题描述】:

我想用下一个子谓词设置复合谓词:

  • id(AND 类型)
  • 名字(或类型)
  • 姓氏(或类型)
  • 中间名(OR 类型)

我正在阅读 NSCompoundPredicate documentation,但理解不够清楚 - 是否可以同时使用 + andPredicateWithSubpredicates:+ orPredicateWithSubpredicates: 将它们作为一个谓词组合到一个提取请求中?

【问题讨论】:

    标签: objective-c nspredicate nsfetchedresultscontroller predicate


    【解决方案1】:

    这样解决:

    对象:

    NSPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[firstPredicate, secondPredicate]];
    NSPredicate *andPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[thirdPredicate, fourthPredicate]];
    NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[orPredicate, andPredicate]];
    [fetchRequest setPredicate:finalPredicate];
    

    斯威夫特:

    let orPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [firstPredicate, secondPredicate])
    let andPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [thirdPredicate, fourthPredicate])
    fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [orPredicate, andPredicate])
    

    【讨论】:

    • 可以将第二步和第三步合并到NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[orPredicate, thirdPredicate, fourthPredicate]]
    猜你喜欢
    • 2018-07-01
    • 2011-03-12
    • 1970-01-01
    • 2015-06-05
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 2011-04-16
    相关资源
    最近更新 更多