Swift - 组合谓词 [英] Swift - Combining Predicates

查看:37
本文介绍了Swift - 组合谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 Swift 中组合谓词?例如:

Is there a way of combining predicates in Swift? For example:

let predicate1 = NSPredicate("self.label = 'foo'"))
let predicate2 = NSPredicate("self.label = 'bar'"))
let combinedPredicate = NSPredicate("self.staticTexts.elementMatchingPredicate(%@).exists AND
  self.staticTexts.elementMatchingPredicate(%@).exists", predicate1, predicate2)

这给了我一个错误,说谓词不能作为其他谓词的参数.有没有其他方法可以将它们结合起来?

That gives me an error saying predicates cannot be arguments to other predicates. Is there another way to combine them?

推荐答案

你需要 NSCompoundPredicate:

let predicate1 = NSPredicate(format: "self.label = 'foo'", argumentArray: [])
let predicate2 = NSPredicate(format: "self.label = 'bar'", argumentArray: [])
let compound = NSCompoundPredicate(andPredicateWithSubpredicates: [predicate1, predicate2])

这篇关于Swift - 组合谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆