gpt4 book ai didi

ios - 添加到 NSCompoundPredicate

转载 作者:可可西里 更新时间:2023-11-01 01:40:39 25 4
gpt4 key购买 nike

我想让我的代码更可用于核心数据,我有一个 fetchRequest,它带有我必须始终使用的谓词。但是,某些方法需要更多条件。我想将这些条件添加到谓词列表中,但我不确定如何执行此操作。我想要一种方法来返回带有基本查询的谓词,然后添加到这些查询中。

 let fetchRequest = NSFetchRequest(entityName: "Stop")

var currentTime = NSDate.getTime()

var sort = NSSortDescriptor(key: "time", ascending: true) // sort by bus stop

fetchRequest.sortDescriptors = [sort]

let predicate = NSPredicate(format: "time >= %ld", currentTime)

let predicate2 = NSPredicate(format: "stop_name == %@", stop)

let predicate3 = NSPredicate(format: "busParent.direction == %@", direction)

let predicate4 = NSPredicate(format: "busParent.name == %@", name)

let predicate5 = NSPredicate(format: "busParent.schedule == %ld", schedule)

fetchRequest.predicate = NSCompoundPredicate.andPredicateWithSubpredicates(
[predicate, predicate2, predicate3, predicate4, predicate5])

// EXAMPLE: HOW WOULD I ADD TO THE COMPOUND PREDICATE ALREADY MADE?
fetchRequest.predicate.????

最佳答案

您可以将 NSCompoundPredicates 与其他 NSPredicates 复合:

    let currentPredicate = fetchRequest.predicate!
let additionalPredicate = NSPredicate(format: "whatever",...)
let nextPredicate = NSCompoundPredicate(type: NSCompoundPredicateType.AndPredicateType, subpredicates: [currentPredicate, additionalPredicate])
fetchRequest.predicate = nextPredicate

通过指定 .AndPredicateType,您会将现有结果“缩小”到那些也满足附加谓词的结果。但您也可以指定 OrPredicateTypeNotPredicateType 以添加或排除(分别)现有结果。

关于ios - 添加到 NSCompoundPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30154442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com