错误:无法将“UInt64”类型的值转换为预期的参数类型“DispatchTime”

时间:2017-09-25 15:56:12

标签: swift swift4

我试图在Swift4中创建一个由变量控制的计时器,唯一的问题是我收到错误:

  

无法将'UInt64'类型的值转换为预期的参数类型'DispatchTime'

以下是代码:

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)
}

根据我的理解,我需要将when变量UInt64转换为DispatchTime

我该怎么做?

1 个答案:

答案 0 :(得分:0)

你应该:

  • DispatchTime.init(uptimeNanoseconds:)-ignorewarnings -keep class * { public private *; } 变量一起使用。

  • 使用此when,因为let when = (DispatchTime.now() + (5 * Double(x)))+次重载。