Making code not runnable on other platforms

77 views Asked by At

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.

2

There are 2 answers

0
Ian On BEST ANSWER

#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 {

} 
0
Narendra G On

Please have a look at syntax for availability checking in swift 2.0

if #available(platform name version, ..., *) {
  statements to execute if the APIs are available
} else {
  fallback statements to execute if the APIs are unavailable
}