4

当我运行以下命令时(2017 年 1 月 7 日):

        let dateComponents = Calendar.current.dateComponents(in: TimeZone.current, from: date)
        print("dateComponents = \(dateComponents)")
        let trigger = UNCalendarNotificationTrigger(dateMatching:dateComponents, repeats: false)
        let nextDate = trigger.nextTriggerDate()
        print("nextDate = \(nextDate)")

然后我得到以下输出:

dateComponents = calendar: gregorian (current) timeZone: Europe/Stockholm (current) era: 1 year: 2017 month: 1 day: 8 hours: 21 minute: 34 seconds: 0 nanosecond: 0 weekday: 1 weekdayOrdinal: 2 Quarter: 0 weekOfMonth : 1 weekOfYear: 1 yearForWeekOfYear: 2017 isLeapMonth: false

下一个日期 = 无

问题:为什么是trigger.nextTriggerDate() = nil

更新:我觉得我dateComponents可能被过度确定了。因此,我介绍了一个nextEvent只包含 dateComponents 的日期小时和分钟的内容:

 var nextEvent = DateComponents()
 nextEvent.day = dateComponents.day
 nextEvent.hour = dateComponents.hour
 nextEvent.minute = dateComponents.minute
        
 let trigger = UNCalendarNotificationTrigger(dateMatching:nextEvent, repeats: false)

当我现在调用trigger.nextTriggerDate()它时

nextDate = Optional(2017-01-08 20:34:00 +0000)

正如它应该。但我不明白为什么在创建触发器时不能使用 dateComponents。

4

1 回答 1

9

在我的测试中,我发现dateComponents.quarter = 0是问题的原因。零季度显然是错误的,应该是quarter = 1似乎有一个旧的错误导致日期组件有一个无效的季度。

如果您手动设置dateComponents.quarter = 1或只是dateComponents.quarter = nil,一切都开始工作。

于 2017-01-07T21:29:49.000 回答