Freddy Benson Freddy Benson - 1 month ago 97
Swift Question

Timer for earlier versions of iOS 10? (Xcode 8, Swift 3)

I'm so confused! When I initialize

NSTimer()
Xcode tells me it's been renamed to
Timer()
and if I initialize
Timer()
Xcode tells me it's only available for iOS 10.0 or later. What am I supposed to use if I want to use a timer for earlier versions of iOS?! The strangest thing to me is that no one else seems to be having this problem, so what am I missing here? What am I doing wrong?

UPDATE:
Alright, even though I already got downvoted a bunch of times because people misunderstood my question (as usual), I'm still going to try to clarify what my situation was. Yes, I understand
NSTimer()
got renamed to
Timer()
in Swift 3, that's literally what I quoted from Xcode's warning message in my first sentence, so I have no idea why everyone felt the urge to 'correct' me on that. Second, it turns out there's only one type method left, which I can use for pre-iOS 10 devices, which is
Timer(timeInterval: <#T##TimeInterval#>, target: <#T##Any#>, selector: <#T##Selector#>, userInfo: <#T##Any?#>, repeats: <#T##Bool#>)
. The other 4 type methods available all pop up a warning that it's only available in iOS 10, as you can see here:
Screenshot of Xcode warning

Also, there is no class method
scheduledTimer()
available, even though I do remember it used to be their in Swift 2.2. There aren't any class methods available to a
timer
object, which means the only available type method mentioned above is the only option I have.

Answer

Timer is NSTimer. It is not new in iOS 10.

if I initialize Timer()

You should never initialize Timer by saying Timer(). You must call the class method scheduledTimer(timeInterval:target:selector:userInfo:repeats:).

What is new in iOS 10 is the class method scheduledTimer(withTimeInterval:repeats:block:). You can't call that in iOS 9 and before. But you don't have to. Just call scheduledTimer(timeInterval:target:selector:userInfo:repeats:) in both iOS 9 and iOS 10, and all will be well.