我的 coredata get
查询的动态 or
谓词
var predicate = [NSPredicate]()
//this three is or
predicate.append(NSPredicate(format: "item_parent_id = %i", 1))
predicate.append(NSPredicate(format: "item_parent_id = %i", 3))
predicate.append(NSPredicate(format: "item_parent_id = %i", 5))
// but this one is and
predicate.append(NSPredicate(format: "price_date CONTAINS[cd] %@", 'timestamp'))
var compound = NSCompoundPredicate.orPredicateWithSubpredicates(predicate)
前三个约束条件是 OR 运算符,最后一个约束条件是 AND
我为此花了很多时间,我是 swift 开发的新手,我不习惯 Objective C ,这就是为什么我很难将我在 objective c 中发现的内容翻译成 swift 的原因。
如果可能,请快速写下答案,任何评论和建议都会有很大的帮助。提前致谢
最佳答案
如果您正在寻找像 (a OR b OR c) AND d
这样的操作,您需要两个复合谓词:
var orList = [NSPredicate]()
//this three is or
orList.append(NSPredicate(format: "item_parent_id = %i", 1))
orList.append(NSPredicate(format: "item_parent_id = %i", 3))
orList.append(NSPredicate(format: "item_parent_id = %i", 5))
let orPredicate = NSCompoundPredicate.orPredicateWithSubpredicates(orList)
// but this one is and
let other = NSPredicate(format: "price_date CONTAINS[cd] %@", 'timestamp')
let compound = NSCompoundPredicate.andPredicateWithSubpredicates([orPredicate, other])
关于ios - AND 和 OR 与 NSPredicate 结合。 iOS swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29749566/