Timer.scheduledTimer Swift 3 pre-iOS 10 compatibility

后端 未结 8 2026
我寻月下人不归
我寻月下人不归 2020-12-15 16:31

I need to schedule a Timer for firing a function every second but I see that in Xcode 8 beta 3 the scheduledTimer is only available for iOS 10.

Is there any alternat

相关标签:
8条回答
  • 2020-12-15 17:06

    The correct form is:

    Timer.scheduledTimer(withTimeInterval: 2, repeats: false){_ in
       "Here your code method"
    }
    
    0 讨论(0)
  • 2020-12-15 17:11

    Here is sample code workable with compatibility:

    if #available(iOS 10.0, *) {
        Timer.scheduledTimer(withTimeInterval: 15.0, repeats: true){_ in
            // Your code is here:
            self.myMethod()
        }
    } else {      
        Timer.scheduledTimer(timeInterval: 15.0, target: self, selector: #selector(self.myMethod), userInfo: nil, repeats: true)
    }
    

    //Your method or function:

    // MARK: -  Method
    
    @objc func myMethod() {
        print("Hi, How are you.")
    }
    
    0 讨论(0)
提交回复
热议问题