Delay using DispatchTime.now() + float?

前端 未结 3 1984
南方客
南方客 2020-12-17 08:30

What exactly is going on with DispatchTime.now()

How come I can\'t assign the time to wait as a variable?

And How Could I use a variable ?

3条回答
  •  感动是毒
    2020-12-17 09:23

    The error message pretty much explains it. You can't add a float and a DispatchTime together as they are different data types.

    When you use this line:

    let when = DispatchTime.now() + 2.2 // <---- THIS IS OKAY
    

    you don't specify what type the 2.2 is and the system concludes it is of type DispatchTime and allows it.

    However when you use this line:

    let whenWhen = DispatchTime.now() + time // <---- THIS IS NOT OKAY
    

    you have already determined that time is a float and that's where the error is generated.

    It's probably easiest to convert like this:

    DispatchTimeInterval.milliseconds(Int(time * 1000))
    

提交回复
热议问题