49

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 ?

Given error >>>

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")
    }
}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Alex
  • 597
  • 1
  • 4
  • 7

3 Answers3

59

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.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Dang that's clean – Alex Dec 29 '17 at 10:57
  • 2
    You don't need the `Double`. Just do: `let time = 2.2` and `time` will be a `Double` by default. – rmaddy Jan 03 '18 at 16:32
  • hey dharmesh can you help me? –  Jan 06 '21 at 06:14
  • @RB1509 Yes, How can I help? – Dharmesh Kheni Jan 06 '21 at 06:16
  • there is a video in view it's actually lottie animation it's work perfectly but ehwn i am going to record that view it will record with some delay(lang). –  Jan 06 '21 at 06:22
  • I can not help much this way. Did you try asking a new question? – Dharmesh Kheni Jan 06 '21 at 06:24
  • my account is actually in banned till last 2 year and i don't know how to remove from ban so can you clear me so i can ask question. –  Jan 06 '21 at 06:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226907/discussion-between-rb1509-and-dharmesh-kheni). –  Jan 06 '21 at 06:31
  • Thanks! The horrible thing about that is Swift compiler returns "Binary operator '+' cannot be applied to operands of type 'DispatchTime' and 'Float'" which is not helpful at all – Paul Ollivier Feb 20 '21 at 09:51
  • @DharmeshKheni, how do you know `DispatchTime` is a `Double`? I don't see that in the definition (`public struct DispatchTime : Comparable { ... }`). I also don't see any `typealias`es. Looks like it is a unique `Comparable` struct. – Andrew Kirna Feb 02 '22 at 21:49
37

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")

}
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
  • There we gooooo Thanks Jayesh Thanki – Alex Dec 29 '17 at 10:50
  • let whenWhen = DispatchTime.now() + DispatchTimeInterval.milliseconds(Int(time*1000)) DispatchQueue.main.asyncAfter(deadline: whenWhen){ print("Hello") } – Alex Dec 29 '17 at 10:52
6

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))
Upholder Of Truth
  • 4,643
  • 2
  • 13
  • 23