在SwiftUI中使用Timer时,为什么我的应用程序崩溃?

时间:2019-07-31 10:55:15

标签: swift xcode swiftui xcode11 ios13

我想在SwiftUI视图中使用带有Timer的视图控制器,但是会崩溃。

class TimeViewController : UIViewController {

    var time = 0
    var timer : Timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(startTimer), userInfo: nil, repeats: true)

    override func viewDidLoad() {
        super.viewDidLoad()
        startTimer()
    }

    @objc func startTimer() {
        time += 1
        print(time)
    }
     }


    struct TimeVC : UIViewControllerRepresentable {
        typealias UIViewControllerType = TimeViewController

        func makeUIViewController(context: UIViewControllerRepresentableContext<TimeVC>) -> TimeVC.UIViewControllerType {
            return TimeViewController()
        }

        func updateUIViewController(_ uiViewController: TimeVC.UIViewControllerType, context: UIViewControllerRepresentableContext<TimeVC>) {
        } }

    struct ContentView : View {
        var body: some View {
            TimeVC()
        } }

0 个答案:

没有答案