Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

xcode - SwiftUI: Type 'Timer' has no member 'publish'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...