ios - AND 和 OR 与 NSPredicate 结合。 iOS swift

标签 ios objective-c swift core-data

我的 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 的原因。

如果可能,请快速写下答案,任何评论和建议都会有很大的帮助。提前致谢

obj c - source #1
obj c - source #2

最佳答案

如果您正在寻找像 (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/

相关文章:

ios - 绘制 UIImage 时出现无效的上下文错误

ios - 如何在 objective-c 中在 friend 的墙上共享多个帖子

ios - NSInputStream 缓冲区的大小

objective-c - UITableViewController 和 UITextField 键盘

ios - 快速:UIPageViewController 操作

swift - 声明字典的字典没有值(value)

ios - UITableViewCell 改变 UITableView 的数据源

ios - 如何根据手机旋转来旋转 Sprite

ios - 自定义 Tableview 单元格宽度问题

ios - Cocos2d : How to check Sprite Frame Name is available or not?