This is a sample app which will show the current time using timer which will show UI updates for every second.
struct ContentView: View {
@State var currentTime = getCurrentTime()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
Text(currentTime)
.font(.system(.largeTitle))
.foregroundColor(.gray)
.onReceive(timer) { _ in
self.currentTime = getCurrentTime()
}
}
}
func getCurrentTime() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "h:mm:ss a"
let dateString = formatter.string(from: Date())
return dateString
}
Sunil Targe • Creator of ॐ • Om chanting with Ninja App
MIT license