gpt4 book ai didi

swift - 核心数据只检索不早于 5 分钟的元素

转载 作者:行者123 更新时间:2023-11-28 15:24:35 27 4
gpt4 key购买 nike

我有一个带有日期属性的实体。现在我只想检索不早于 5 分钟的条目。

到目前为止我试过了。

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ContactRequest")

// Create a sort descriptor object that sorts on the "title"
// property of the Core Data object
let sortDescriptor = NSSortDescriptor(key: "request_time", ascending: true)

// Set the list of sort descriptors in the fetch request,
// so it includes the sort descriptor
fetchRequest.sortDescriptors = [sortDescriptor]

let originalDate = Date() // "Jun 13, 2016, 1:23 PM"
let calendar = Calendar.current
let limitDate = calendar.date(byAdding: .minute, value: -5, to: originalDate, options: [])

// Create a new predicate that filters out any object that
// doesn't have a title of "Best Language" exactly.
let predicate = NSPredicate(format: "request_time > %@", limitDate)

// Set the predicate on the fetch request
fetchRequest.predicate = predicate

但是 xCode 说 calendar.date(byAdding: .minute, value: -5, to: originalDate, options: []) 不可用。

最佳答案

看来你的api有误。变化

let limitDate = calendar.date(byAdding: .minute, value: -5, to: originalDate, options: [])

let limitDate = calendar.date(byAdding: .minute, value: -5, to: originalDate, wrappingComponents: true)

见: https://developer.apple.com/documentation/foundation/calendar/2293453-date

我不确定为什么你有 options 而不是 wrappingComponents。可能是某个swift版本的api变了

关于swift - 核心数据只检索不早于 5 分钟的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45456098/

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