Timer.scheduledTimer Swift 3 pre-iOS 10 compatibility

后端 未结 8 2076
我寻月下人不归
我寻月下人不归 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条回答
  •  旧时难觅i
    2020-12-15 16:57

    Updated for swift 3:

    If you want to use Timer for some delay or any other purpose used below lines of code in your project;

    // function defination:

    func usedTimerForDelay()  {
        Timer.scheduledTimer(timeInterval: 0.3,
                             target: self,
                             selector: #selector(self.run(_:)),
                             userInfo: nil, 
                             repeats: false)
    }
    
    func run(_ timer: AnyObject) {
          print("Do your remaining stuff here...")
    
    }
    

    // function call:

    self.usedTimerForDelay()
    

    NOTE:- you can change the time interval as you want.

    //Enjoy coding..!

提交回复
热议问题