Hello! I am wanting to have the current time updating in SwiftUI I want to embed this in another view. After scouring the internet, this is what I've managed to put together so far.
Here's where I've got so far:
I don't have loads of experience – any and all help is appreciated, and I thank-you all for your time.
Here's where I've got so far:
Code Block struct Time : View { static let timeFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateStyle = .none formatter.timeStyle = .medium return formatter }() var now = Date() @State var timer: Timer @State var repeater: Bool = true var body: some View { Text("\(now, formatter: Self.timeFormatter)") } } }
I don't have loads of experience – any and all help is appreciated, and I thank-you all for your time.
I didn't know it sometimes does not work.
I'm using xcode 12.0-beta-3, target ios 13.5 and 14, tested on a few simulators and real devices, ipad(ios 14) and iphone(ios 13.5).
I cannot see any problems with the following (updated) test:
What setup does it fail on?
I'm using xcode 12.0-beta-3, target ios 13.5 and 14, tested on a few simulators and real devices, ipad(ios 14) and iphone(ios 13.5).
I cannot see any problems with the following (updated) test:
Code Block import SwiftUI struct ContentView: View { var body: some View { TestView().padding() } } struct TestView: View { @State var timeNow = "" let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() var dateFormatter: DateFormatter { let fmtr = DateFormatter() fmtr.dateFormat = "LLLL dd, hh:mm:ss a" return fmtr } var body: some View { Text("Currently: " + timeNow) .onReceive(timer) { _ in self.timeNow = dateFormatter.string(from: Date()) } } }
What setup does it fail on?