Matt121 Matt121 - 24 days ago 10
Swift Question

Error: Cannot convert value of type 'UInt64' to expected argument type 'DispatchTime'

Im trying to create a timer in Swift4 that is controlled by a variable, the only issue is that I get a error:


Cannot convert value of type 'UInt64' to expected argument type 'DispatchTime'


Here is the code:

let maxNumber = maxNumberField.intValue
let amountOfNumbers = amountOfNumbersField.intValue
var delay = 5
var x: Int32 = amountOfNumbers

while(x > 0){

let when = (DispatchTime.now().uptimeNanoseconds + (5 * UInt64(x)))
DispatchQueue.main.asyncAfter(deadline: when) { // error

let number = arc4random_uniform(UInt32(maxNumber + 1))
let synth = NSSpeechSynthesizer()
synth.startSpeaking(String(number))
}
x = (x - 1)
}


From my understanding I need to convert the
when
variable which is an
UInt64
to an
DispatchTime
.

How would I do this?

Answer Source

You should either: