macosswiftui

Update variables in ContentView at midnight


I have the following variables in my ContentView file

let DOY = getDayOfYear()
let DRNoW = daysinyear()
let WN = weekNumber()
let DN = dayNumber()

And using the func of:

func getDayOfYear() -> Int {
    let date = Date() // now
    let cal = Calendar.current
    let day1 = cal.ordinality(of: .day, in: .year, for: date)!

    return day1
}

I want the variables to update at midnight in the ContentView.

How do I make it happen?


Solution

  • Took a long time to figure out.

      NotificationCenter.default.addObserver(self, selector: #selector(updateGroupxx(notification:)), name: .NSCalendarDayChanged, object: nil)