When I run the following (on January 7 2017):
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)")
then I get the following output:
dateComponents = calendar: gregorian (current) timeZone: Europe/Stockholm (current) era: 1 year: 2017 month: 1 day: 8 hour: 21 minute: 34 second: 0 nanosecond: 0 weekday: 1 weekdayOrdinal: 2 quarter: 0 weekOfMonth: 1 weekOfYear: 1 yearForWeekOfYear: 2017 isLeapMonth: false
nextDate = nil
Question: why is trigger.nextTriggerDate() = nil
?
UPDATE: I had the feeling that my dateComponents
could be overdetermined. I therefore introduced a nextEvent
that only contained the day hour and minute of the dateComponents:
var nextEvent = DateComponents()
nextEvent.day = dateComponents.day
nextEvent.hour = dateComponents.hour
nextEvent.minute = dateComponents.minute
let trigger = UNCalendarNotificationTrigger(dateMatching:nextEvent, repeats: false)
when I now invoke trigger.nextTriggerDate()
it becomes
nextDate = Optional(2017-01-08 20:34:00 +0000)
as it should. But I do not understand why I cannot use the dateComponents when I create the trigger.
During my tests I found dateComponents.quarter = 0
to be the cause of the problems. Zero quarter is obviously wrong, it should be quarter = 1
It seems there is an old bug that causes the date components to have an invalid quarter.
If you manually set dateComponents.quarter = 1
or just dateComponents.quarter = nil
, everything starts to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With