為什么我不能指定等待變量的時間?
我怎么能使用變量趴捅?
給定錯誤>>>
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()是一個雙精度數(shù).您不能將float和double值一起添加. (關(guān)于為什么不能添加不同類型的值的小解釋,可以找到?here).
更換
var time: Float = 2.2
同
var time: Double = 2.2
它會工作正常.