Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift4 二进制运算符'+';无法应用于类型为';调度时间';和';Int32';_Swift4 - Fatal编程技术网

Swift4 二进制运算符'+';无法应用于类型为';调度时间';和';Int32';

Swift4 二进制运算符'+';无法应用于类型为';调度时间';和';Int32';,swift4,Swift4,我试图在Swift4中为计时器设置可变时间延迟,但当我输入该变量时,得到错误: Binary operator '+' cannot be applied to operands of type 'DispatchTime' and 'Int32' 我使用了代码: let when = (DispatchTime.now() + (5 * x)) 变量“x”是一个Int32 如果您知道如何修复它,请提供帮助。您可以这样做: let x: Int32 = 2 let when = (Dispa

我试图在Swift4中为计时器设置可变时间延迟,但当我输入该变量时,得到错误:

Binary operator '+' cannot be applied to operands of type 'DispatchTime' and 'Int32'
我使用了代码:

let when = (DispatchTime.now() + (5 * x))
变量“x”是一个Int32

如果您知道如何修复它,请提供帮助。

您可以这样做:

let x: Int32 = 2
let when = (DispatchTime.now().uptimeNanoseconds + (5 * UInt64(x)))
问题是不能对不同的类型求和。DispatchTime使用64位(无符号)表示,因此可以使用
UInt64(x)
强制转换它

要从DispatchTime获取
UInt64
,您可以使用
uptimeNanoseconds
rawValue

var dispatchAfter = DispatchTimeInterval.seconds(1)

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + dispatchAfter, execute: {
    // Do your thing
})