【问题标题】:AND and OR combined with NSPredicate. IOS SwiftAND 和 OR 与 NSPredicate 结合。 IOS 斯威夫特
【发布时间】:2015-04-20 13:40:05
【问题描述】:

我的核心数据 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 运算符,最后一个约束用于日期与

我为此花了很多时间,我是 swift 开发的新手,我不习惯 Objective C,这就是为什么我很难将我在 Objective C 中发现的内容翻译成 swift。

如果可能的话 swift 写下答案,任何评论和建议都会有很大帮助。先谢谢了

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

【问题讨论】:

  • “OR”部分可以简化为单个谓词NSPredicate(format: "item_parent_id IN %@", [1, 3, 5]) ...

标签: ios objective-c swift core-data


【解决方案1】:

如果你正在寻找像(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])

【讨论】:

  • 哇,没想到这么简单,我试试..谢谢先生
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 2017-07-22
  • 2017-03-24
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多