Timer.scheduledTimer Swift 3 pre-iOS 10 compatibility

rockdaswift picture rockdaswift · Aug 1, 2016 · Viewed 56.4k times · Source

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 alternative for using the timer in iOS 9 or previous versions?

Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in print("Hi!")})

Answer

rockdaswift picture rockdaswift · Aug 1, 2016

Solved using

Timer.scheduledTimer(timeInterval: 1,
                           target: self,
                         selector: #selector(self.updateTime),
                         userInfo: nil,
                          repeats: true)