I am limited to Xcode 6.2 if I am using Mavericks, and the following code:
let setOfNumbers: Set<Int> = [1, 2, 3, 4, 5];
won't work. So Set
should only work in Swift 1.2 and above. How do I print out what Swift version I am using in the program? (just like p RUBY_VERSION
if using Ruby)
Switching Swift Toolchain It can be done using Xcode, Go to Xcode->Preferences->Components-> Toolchains section and Select the Swift. 3.1. 1 snapshot, this will set the new Swift version for the local Xcode. You may need to restart Xcode.
Swift 5.1 was officially released in September 2019. Swift 5.1 builds on the previous version of Swift 5 by extending the stable features of the language to compile-time with the introduction of module stability.
Updated answer
Starting with Swift 2.2 (Xcode 7.3b) you can determine the Swift version and run code conditionnally with the #if swift()
build configuration.
Example:
#if swift(>=2.2)
print("Running Swift 2.2 or later")
#else
print("Running Swift 2.1 or earlier")
#endif
The branch containing code for the compatible version of Swift will be executed, and the other branch will be ignored.
Old answer
There's no known way to do it in code (ref, ref).
You can do this in the Terminal:
swift -version
For this command to be accurate you need to check that Xcode tools are linked to the proper Xcode version (can be confusing if you have Xcode and Xcode-beta installed side by side).
This is the way you can check in Terminal:
$ xcrun swift -version
It will gives you result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With