this is the way I implement a timer.
@State private var timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
Do you have imported SwiftUI?
Did you also implement the timer before the body part?
I don't know why but maybe you need @State before the variable declaration. I also think a Timer can not be a let
. I think you have to use var
-
Edit:
I think I know what the problem is. You named the struct
"Timer". When you want so create the Timer
Swift try to call .publish
on the View Timer
rather than the Timer
you actually want. Rename the View
to TimerView(or something else) and try again.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…