SwiftUI Stopwatch

I was trying to follow a SwiftUI tutorial and build a simple stopwatch app.
Code Block
class TimeBreakdown: ObservableObject {
  @Published var hours = 0
  @Published var minutes = 0
  @Published var secondsElapsed = 0
}
class StopwatchManager: ObservableObject {
  @Published var mode: StopwatchMode = .stopped
  var timer = Timer()
  @ObservedObject var timeKeeper = TimeBreakdown()

I have a Text view that displays the time by calling
Code Block
stopwatch.timekeeper.hours
and so on. However, it isn't updating the view right now after the timer starts running and I was wondering how I can fix that
Can you show the code where you update ?
Code Block
func start() {
    mode = .running
    timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {timer in
      self.timekeeper.secondsElapsed += 1;
      if (self.timekeeper.secondsElapsed > 59) {
        self.timekeeper.secondsElapsed = 0
        self.timekeeper.minutes += 1
      }
      if (self.timekeeper.minutes > 59) {
        self.timekeeper.minutes = 0
        self.timekeeper.hours += 1
      }
    }
  }

Having @ObservedObject in a non-View type has no effect.
In other words, changes in the nested @ObservedObject does not cause UI updating.

how I can fix that

An example, make your TimeBreakdown a struct.
Code Block
struct TimeBreakdown {
var hours = 0
var minutes = 0
var secondsElapsed = 0
}

And add @Published to the property holding it.
Code Block
class StopwatchManager: ObservableObject {
@Published var mode: StopwatchMode = .stopped
var timer: Timer?
@Published var timeKeeper = TimeBreakdown() //<- Is this `timeKeeper` or `timekeeper`?
//...
}


SwiftUI Stopwatch
 
 
Q