8

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!

Community
  • 1
  • 1
Dershowitz123
  • 3,722
  • 3
  • 18
  • 36
  • `let when = dispatch_time(DISPATCH_TIME_NOW, delay)` – iphonic Jul 15 '16 at 04:49
  • 1
    Have you tried if `DispatchTime.now()` runs on iOS 9? Some availability annotations are wrong. – Martin R Jul 15 '16 at 05:03
  • 1
    why you need check iOS10 to use swift3 syntax? swift3 is really support for iOS lower version like 7 8 9. If you want to compile in new Xcode8 with swift3, just write only code in swift 3 syntax. all your code you need: func delay( _ delay: Double, closure: () -> ()){ DispatchQueue.main.after(when: DispatchTime.now() + delay, execute: closure) } – larva Jul 15 '16 at 05:13
  • @MartinR no I've not checked on a phone. I just tried it in playground and I got those errors. When I command clicked on the 'DisptachTime' and 'now()' , it said available in iOS 10 and macOS 10.12. So I thought I'll try API availability and then these errors showed up. :( – Dershowitz123 Jul 15 '16 at 05:34
  • @Dershowitz123: As I said: the availability annotations are sometimes wrong. The Swift 3 code should run on all platforms where Swift 3 is available. – `dispatch_after` is Swift 2 and won't compile in Swift 3. – Martin R Jul 15 '16 at 05:39
  • So what do i do in case I am writing an app for both iOS 10 AND – Dershowitz123 Jul 15 '16 at 05:42
  • @Dershowitz123: Swift 3 runs on iOS 8 and later. If you want to compile for iOS 7 then you have to use Swift 2.2 with Xcode 7.3.1, and then we are talking about *source code compatibility*, not API availability. Is that what you are really asking for? But keep in mind that Swift 2.2 and Swift 3 are *very* different (and most iOS devices run iOS 8 or later). – Martin R Jul 15 '16 at 05:50
  • `DispatchTime.now()` is Swift 3 so It will work for iOS 8, 9, 10. It will not support iOS 7 or previous versions http://stackoverflow.com/questions/38092144/swift-3-0-or-swift-2-3-minimum-system-version-requirement-and-is-it-deployed-in/38092257#38092257 – Ashish Kakkad Jul 15 '16 at 06:26
  • @AshishKakkad, Swift 3 does support iOS 7. I've added a comment to your answer. – Cœur May 27 '17 at 01:33

1 Answers1

3

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

Cœur
  • 32,421
  • 21
  • 173
  • 232