gpt4 book ai didi

ios - 如何按 nextTriggerDate 对 UNNotificationRequests 数组进行排序

转载 作者:行者123 更新时间:2023-11-29 11:34:51 24 4
gpt4 key购买 nike

我有一组 UNNotificationRequest。我想按 nextTriggerDate 对它们进行排序。

据我所知,我会使用 array.sorted(by:predicate)

对数组进行排序

让 sortedNotifications = notificationRequests.sorted(by:
{ $0.trigger.nextTriggerDate?.compare($1.trigger.nextTriggerDate!) == .orderedAscending })

但是,问题是 .trigger 没有 nextTriggerDate 属性。

为了获得nextTriggerDate,我必须提取触发器并将其转换为UNCalendarNotificationTrigger。据我所知,这不能在谓词中完成。

有什么想法吗?

最佳答案

您可以使用 UNNotificationRequest 和 nextTriggerDate (UNNotificationRequest,nextTriggerDate)

创建 元组
// get request with date Tuple -->  example : (value0,value1)

let requestWithDateTuple = notificationRequests.map({ (req) -> (UNNotificationRequest,Date?)? in
guard let trigger = req.trigger as? UNCalendarNotificationTrigger else {
return nil
}
return (req,trigger.nextTriggerDate())
}).compactMap({$0})

// you will get Tuple (request,Date) ,sort them by date
let sortedTuple = requestWithDateTuple.sorted(by: { $0.1?.compare($1.1!) == .orderedAscending })

// sorted request only
let requestSorted = sortedTuple.map({$0.0})

关于ios - 如何按 nextTriggerDate 对 UNNotificationRequests 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50278950/

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