Checking when a date has passed - Swift
Implement to observe
NSCalendarDayChangedNotification
Posted whenever the calendar day of the system changes, as determined by the system calendar, locale, and time zone. This notification does not provide an object.
If the the device is asleep when the day changes, this notification will be posted on wakeup. Only one notification will be posted on wakeup if the device has been asleep for multiple days.
There are no guarantees about the timeliness of when this notification will be received by observers. As such, you should not rely on this notification being posted or received at any precise time.
The notification is posted through
[NSNotificationCenter defaultCenter]
.
Example:
In applicationDidFinishLaunching
add
NSNotificationCenter.defaultCenter().addObserver(self, selector:"calendarDayDidChange:", name:NSCalendarDayChangedNotification, object:nil)
and implement the method
func calendarDayDidChange(notification : NSNotification)
{
doSomethingWhenDayHasChanged()
}
or use the block API.
If the class including the observer is not the application delegate class you might remove the observer at some time.
Update vadian's reply to Swift 5:
NotificationCenter.default.addObserver(self, selector:#selector(self.calendarDayDidChange(_:)), name:NSNotification.Name.NSCalendarDayChanged, object:nil)
and implement the method
@objc private func calendarDayDidChange(_ notification : NSNotification) {
doSomethingWhenDayHasChanged()
}