In accordance with this brilliant answer on the usage of dispatch_after
by @matt, I tried the code on the playground and it works fine (no errors). But when I try to do a backward compatibility as DispatchTime.now()
is available only for iOS 10
only like so:
func delay( _ delay: Double, closure: () -> ()){
guard #available(iOS 10, *) else {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
return
}
let when = DispatchTime.now() + delay
DispatchQueue.main.after(when: when, execute: closure)
}
The compiler offers to fix the DISPATCH_TIME_NOW
to Replace "DISPATCH_TIME_NOW" with "dispatch_time_t(DISPATCH_TIME_NOW)"
and throws an error saying
Cannot convert value of type 'Int' to expected argument type 'dispatch_time_t' (aka 'UInt64')
I tried fixing it as the compiler offers but finally ended up with more errors. How should I use the backward compatibility here? What wrong I'm I doing? Do help, thanks!
DispatchTime.now()
, as well as almost any DispatchQueue
or DispatchTime
method, is available for iOS 7, 8, 9 and 10. Documentation is simply incorrect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With