Aej11 Aej11 - 1 month ago 10
Swift Question

How to use NSCalendarDayChanged - swift

The following code isn't working (isn't activating calendarDayDidChange) when the date changes:

// Define identifier
let notificationName = Notification.Name("NSNotification.Name.NSCalendarDayChanged")

// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(calendarDayDidChange(notification:)), name: notificationName, object: nil)


The method header is

func calendarDayDidChange(notification : NSNotification)

Answer

NSCalendarDayChanged is already of type NSNotification.Name so you don't need to cast it.

let notificationName: Notification.Name = .NSCalendarDayChanged

or

let notificationName = Notification.Name.NSCalendarDayChanged
Comments