当我运行以下命令时(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。