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.
Asked
Active
Viewed 56 times
0

vrwim
- 13,020
- 13
- 63
- 118
-
So you only want code to execute on any version of iOS, but not at all on OSX? or something like that? – Ian Jun 09 '15 at 21:44
-
@Ian Yes, that is what I want – vrwim Jun 09 '15 at 21:45
2 Answers
1
#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 {
}

Ian
- 12,538
- 5
- 43
- 62
0
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
}

Narendra G
- 491
- 4
- 9