当前位置 : 主页 > 手机开发 > ios >

AND和OR结合NSPredicate. IOS斯威夫特

来源:互联网 收集:自由互联 发布时间:2021-06-11
我的coredata获取查询的动态或谓词 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
我的coredata获取查询的动态或谓词

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中写的快速翻译.

如果可能的话,快速写下答案,任何评论和建议都将是一个很大的帮助.提前致谢

obj c – source #1
obj c – source #2

如果你正在寻找像(a OR b OR c)和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])
网友评论