I know I can find the version of Swift I'm running right now reverting to a Terminal and typing:
xcrun swift --version
Swift version 1.1 (swift-600.0.57.4)
Target: x86_64-apple-darwin13.4.0
Also, I've been reading about the Preprocessor Macros in Swift, but no luck finding a Swift version constant.
As Swift 1.2 approaches it will be nice to flag old code that only runs on Swift 1.1 (Xcode up to 6.2) or new code that needs Xcode 6.3 (Swift 1.2)
Note: I can also use system() to do something like:
system("xcrun swift --version | grep version > somefile.txt")
Then open somefile.txt, but rather prefer some simpler solution
You can use conditional compilation directives to test for the specific Swift version used to build your project:
From your comment:
You should not check the version of your programming language in order to use some features or not. This approach is much better:
Just check whether a method is available or not.
For iOS :
For OSX :
K.
Swift 3.1 extends the
@available
attribute to support specifying Swift version numbers in addition to its existing platform versions.Finally got a workaround to do this. I'm using the constants prefixed with
__
you can observe in your Playground. This would have been easier with some level of reflection, but...__IPHONE_OS_VERSION_MAX_ALLOWED
is 80200, meaning__IPHONE_8_2
for Xcode 6.2 (Swift 1.1) but its value is 80300 (__IPHONE_8_3
) in Xcode 6.3 (Swift 1.2)So now in your library you can fail fast and tell your user Swift's version is not correct using this:
Swift will give you a nice:
UPDATE WWDC 2015 - Swift 2.0
As stated in Apple's Swift blog, in Swift 2.0 we have
#available
blocks to check for certain OS versions in our code. An example should be: