Phillip Phillip - 17 days ago 7
Swift Question

Xcode Swift 3 Timer Error

This code had to be converted to Swift 3.

func checkIfCorrect (_ buttonPressed:Int) {
if buttonPressed == playlist[numberOfTaps] {
if numberOfTaps == playlist.count - 1 { // we have arrived at the last item of the playlist

let time = DispatchTime(uptimeNanoseconds: DispatchTime.now()) + Double(Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)

DispatchQueue.main.asyncAfter(deadline: time, execute: {
self.nextRound()
})

return
}


The code returns an error that says "Cannot convert value of type 'DispatchTime' to expected argument type UInt64". What would be a good fix for this?

Answer

In Swift 3 it's much easier

For example a DispatchTime of a second from now is:

let time : DispatchTime = .now() + .seconds(1)

These other units are available, too:

  • .milliseconds()
  • .microseconds()
  • .nanoseconds()