Phillip Phillip - 1 year ago 125
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: + Double(Int64(NSEC_PER_SEC)) / Double(NSEC_PER_SEC)

DispatchQueue.main.asyncAfter(deadline: time, execute: {


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 Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download