0

I followed DuncanC's solution in Swift timer in milliseconds. Code below:

startTime = NSDate.timeIntervalSinceReferenceDate
Timer.scheduledTimer(timeInterval: 0.02,
                                           target: self,
                                           selector: Selector(("advanceTimer:")),
                                           userInfo: nil,
                                           repeats: true)


func advanceTimer(timer: Timer)
{
    //Total time since timer started, in seconds
    self.time = NSDate.timeIntervalSinceReferenceDate - startTime
    //The rest of your code goes here
}

Xcode recommended I change NSDate.timeIntervalSinceReferenceDate() to NSDate.timeIntervalSinceReferenceDate and startTime: NSTimeInterval to startTime: TimeInterval for Swift 3.0. My code produces the error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyGame.GameScene advanceTimer:]: unrecognized selector sent to instance

Community
  • 1
  • 1
iGetIt
  • 695
  • 5
  • 20
  • Why are you using `NSDate` instead of `Date`? – rmaddy Nov 11 '16 at 15:14
  • thanks for the correction. I managed to get the formatting from http://stackoverflow.com/questions/28872450/conversion-from-nstimeinterval-to-hour-minutes-seconds-milliseconds-in-swift – iGetIt Nov 11 '16 at 15:21

2 Answers2

3

In Swift 3 Selector syntax is changed, now you need to pass first parameter label with your action, your selector should be like #selector(self.advanceTimer(timer:)).

Timer.scheduledTimer(timeInterval: 0.02,
                     target: self,
                     selector: #selector(self.advanceTimer(timer:)),
                     userInfo: nil,
                     repeats: true)
Nirav D
  • 71,513
  • 12
  • 161
  • 183
1

advanceTimer(timer:) should be declared differently according to Swift API Design Guidelines, You should use Swift's native Date instead of NSDate and selector syntax has changed so your code would now be written like this:

startTime = NSDate.timeIntervalSinceReferenceDate
Timer.scheduledTimer(timeInterval: 0.02,
                     target: self,
                     selector: #selector(advanceTimer),
                     userInfo: nil,
                     repeats: true)

func advanceTimer(_ timer: Timer)
{
    //Total time since timer started, in seconds
    self.time = NSDate.timeIntervalSinceReferenceDate - startTime
    //The rest of your code goes here
}

Also, Timer has new closure based methods, so your the whole thing can now be reduced to:

startTime = NSDate.timeIntervalSinceReferenceDate
Timer.scheduledTimer(withTimeInterval: 0.02, repeats: true) { timer in
    //Total time since timer started, in seconds
    self.time = NSDate.timeIntervalSinceReferenceDate - startTime
    //The rest of your code goes here
}
jjatie
  • 5,152
  • 5
  • 34
  • 56
  • +1 for the new closure-based timers. I wish Apple had added that in iOS 4, when they first introduced blocks. Not sure why it took so long. I didn't discover this addition until a few days ago. (Note that if your app needs to run on iOS versions before iOS 10 then you can't use closure-based timers. :( – Duncan C Nov 11 '16 at 19:52