【问题标题】:How to sort an array of UNNotificationRequests by nextTriggerDate如何按 nextTriggerDate 对 UNNotificationRequests 数组进行排序
【发布时间】:2018-10-21 01:57:33
【问题描述】:

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

据我了解,我会使用 array.sorted(by:predicate) 对数组进行排序

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

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

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

有什么想法吗?

【问题讨论】:

    标签: ios sorting nspredicate unnotificationrequest unnotificationtrigger


    【解决方案1】:

    您可以使用 UNNotificationRequest 和 nextTriggerDate (UNNotificationRequest,nextTriggerDate) 创建Tuple

    // 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})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 2016-11-07
      • 2017-11-20
      • 2021-09-21
      • 2021-11-03
      • 2020-11-02
      相关资源
      最近更新 更多