Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print out Swift's version in a program?

Tags:

swift

swift2

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)

like image 235
nonopolarity Avatar asked Aug 31 '15 13:08

nonopolarity


People also ask

How do I change the version of Swift?

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.

What is the latest Swift version?

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.


2 Answers

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).

like image 128
Eric Aya Avatar answered Oct 08 '22 17:10

Eric Aya


This is the way you can check in Terminal:

$ xcrun swift -version

It will gives you result.

like image 28
Murali Avatar answered Oct 08 '22 18:10

Murali