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)
}
}
}
Post
Replies
Boosts
Views
Activity
Sorry,
I get the same problem with macOS11 and Xcode 12 with your code. Sounds like something they just fixed in Xcode 13.
I don't want to upgrade to macOS12 beta yet.
I'm still using Xcode 12 and macOS 11.
Nope. I replaced it with Text("Hi").
Same problem.
Ok. Found out I should use tags instead.
https://stackoverflow.com/questions/35054432/differentiate-between-multiple-uibuttons-with-empty-titles