Post

Replies

Boosts

Views

Activity

Reply to Updating Time - SwiftUI
Here is something I ripped up and works. I deliberately reduced the timer interval to 0.1 to give a more accurate seconds update. You can download the code in https://github.com/richardlamo/SwitfUI-Clock          let clockTimer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()     let calendar = Calendar.current     @State var currentDate = Date()     @State var hour : NSInteger = 0     @State var minute : NSInteger = 0     @State var second : NSInteger = 0          let cyclePeriod = 0.5          var body: some View {         ZStack {             VStack {                 Text("\(hour):\(minute):\(second)")                       }         }         .onReceive(clockTimer) {             time in             currentDate = time             hour = calendar.component(.hour, from: currentDate)             minute = calendar.component(.minute, from: currentDate)             second = calendar.component(.second, from: currentDate)         }     } }
May ’22