为什么我不能指定等待变量的时间?
我怎么能使用变量?
给定错误>>>
Binary operator ‘+’ cannot be applied to operands of type
‘dispatchTime’ and ‘Float’
var time : Float = 2.2 // <---time
@IBAction func buttonpressed(_ sender: Any) {
let when = dispatchTime.Now() + 2.2 // <---- THIS IS OKAY
dispatchQueue.main.asyncAfter(deadline: when){
print("Hello")
}
let whenWhen = dispatchTime.Now() + time // <---- THIS IS NOT OKAY
dispatchQueue.main.asyncAfter(deadline: whenWhen){
print("Hello")
}
}
dispatchTime.Now()是一个双精度数.您不能将float和double值一起添加. (关于为什么不能添加不同类型的值的小解释,可以找到 here).
更换
var time: Float = 2.2
同
var time: Double = 2.2
它会工作正常.