Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DISPATCH_TIME_NOW in swift 3 and Backward compatibility

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!

like image 752
Mtoklitz113 Avatar asked Jul 15 '16 04:07

Mtoklitz113


1 Answers

DispatchTime.now(), as well as almost any DispatchQueue or DispatchTime method, is available for iOS 7, 8, 9 and 10. Documentation is simply incorrect.

like image 176
Cœur Avatar answered Oct 20 '22 16:10

Cœur