DispatchTime.now()究竟发生了什么 为什么我不能指定等待变量的时间? 我怎么能使用变量? 给定错误 Binary operator ‘+’ cannot be applied to operands of type ‘DispatchTime’ and ‘Float’ var time : Fl
为什么我不能指定等待变量的时间?
我怎么能使用变量?
给定错误>>>
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
它会工作正常.