Search code examples
swift2availability

Making code not runnable on other platforms


In Swift 2, how can I use the #available condition to prevent a block of code from executing on a certain platform? The * seems to allow the version you specify in your deployment target. And specifying iOS Int.max doesn't work.


Solution

  • #available is used for specifying certain versions on certain platforms. If you only want to limit your code to a certain platform, you can use compiler directives.

    #if os(iOS)
        // do stuff only on iOS
    #elseif os(OSX)
        // do stuff only on OS X
    #endif
    

    But I believe the reason what you were trying to do with Int.max wasn't working because it requires an UInt32 literal (i.e. up to 4294967295 which is (2^32) - 1 or UInt32.max -1):

    if #available(iOS 1000, watchOS 1000, *) {
        // Should execute only on OSX higher than deployment target
    } else {
    
    }