Swift 2.2 introduced a new
#if swift build configuration option, which lets you compile certain code only if a specific version of the Swift compiler is detected. This is particularly useful for libraries that need to support multiple incompatible versions of Swift at the same time, because only one version of their code will ever be compiled.
In the example below, the
print() code will be compiled, but the capital letter text will be completely ignored – but only because we're using a Swift 2.2 compiler:
#if swift(>=2.2) print("Running Swift 2.2 or later") #else THIS WILL COMPILE JUST FINE IF YOU'RE USING A SWIFT 2.2 COMPILER BECAUSE THIS BIT IS COMPLETELY IGNORED! #endif
Available from iOS 7.0
Did this solution work for you? Please pass it on!
Other people are reading…
About the Swift Knowledge Base
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Help support Hacking with Swift
This site is funded by Hacking with Swift supporters who buy my e-books. If you can, please support my work – it comes packed with bonus material!