[英]AND and OR combined with NSPredicate. IOS Swift
我的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
我花了很多时间来做这件事,我是快速发展的新手,我不习惯于目标C,这就是为什么我很难转换为我在客观c中写的快速翻译。
如果可能的话,快速写下答案,任何评论和建议都将是一个很大的帮助。 提前致谢
如果你正在寻找像(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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.