Delay using DispatchTime.now() + float?

DispatchTime.now() is a double. You cannot add a float and a double value together. (A small explanation about why can't you add values with different types can be found here).

Replace

var time: Float = 2.2

with

var time: Double = 2.2

And it will work fine.


You can use DispatchTimeInterval to add time in DispatchTime.

For Example :

let whenWhen = DispatchTime.now() + DispatchTimeInterval.seconds(time)
DispatchQueue.main.asyncAfter(deadline: whenWhen){

    print("Hello")

}

Tags:

Swift