Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I see which version of Swift I'm using?

I just created a new Swift project within Xcode. I am wondering which version of Swift it's using.

How can I see, in Xcode or the terminal, what version of Swift I am using inside my project?

like image 750
David Snabel Avatar asked Jun 11 '15 19:06

David Snabel


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 version of Swift is in Xcode 12?

Xcode 12 contains the next Swift update bundled, version 5.3. The more the Swift language advances, the more features we all get, resulting to better, safer, clearer and more robust code. Once again Swift brings improvements that most of them will be proved useful to all developers.

Is Swift installed with Xcode?

Swift 5.4. Swift 5.4 is available as part of Xcode 12.5.


2 Answers

What I do is say in the Terminal:

$ xcrun swift -version 

Output for Xcode 6.3.2 is:

Apple Swift version 1.2 (swiftlang-602.0.53.1 clang-602.0.53) 

Of course that assumes that your xcrun is pointing at your copy of Xcode correctly. If, like me, you're juggling several versions of Xcode, that can be a worry! To make sure that it is, say

$ xcrun --find swift 

and look at the path to Xcode that it shows you. For example:

/Applications/Xcode.app/... 

If that's your Xcode, then the output from -version is accurate. If you need to repoint xcrun, use the Command Line Tools pop-up menu in Xcode's Locations preference pane.

like image 187
matt Avatar answered Sep 28 '22 04:09

matt


Project build settings have a block 'Swift Compiler - Languages', which stores information about Swift Language Version in key-value format. It will show you all available (supported) Swift Language Version for your Xcode and active version also by a tick mark.

Project ► (Select Your Project Target) ► Build Settings ► (Type 'swift_version' in the Search bar) Swift Compiler Language ► Swift Language Version ► Click on Language list to open it (and there will be a tick mark on any one of list-item, that will be current swift version).

Look at this snapshot, for easy understanding:

xcode with described areas highlighted


With help of following code, programmatically you can find Swift version supported by your project.

#if swift(>=5.3) print("Hello, Swift 5.3")  #elseif swift(>=5.2) print("Hello, Swift 5.2")  #elseif swift(>=5.1) print("Hello, Swift 5.1")  #elseif swift(>=5.0) print("Hello, Swift 5.0")  #elseif swift(>=4.2) print("Hello, Swift 4.2")  #elseif swift(>=4.1) print("Hello, Swift 4.1")  #elseif swift(>=4.0) print("Hello, Swift 4.0")  #elseif swift(>=3.2) print("Hello, Swift 3.2")  #elseif swift(>=3.0) print("Hello, Swift 3.0")  #elseif swift(>=2.2) print("Hello, Swift 2.2")  #elseif swift(>=2.1) print("Hello, Swift 2.1")  #elseif swift(>=2.0) print("Hello, Swift 2.0")  #elseif swift(>=1.2) print("Hello, Swift 1.2")  #elseif swift(>=1.1) print("Hello, Swift 1.1")  #elseif swift(>=1.0) print("Hello, Swift 1.0")  #endif 

Here is result using Playground (with Xcode 11.x)

enter image description here

like image 41
Krunal Avatar answered Sep 28 '22 06:09

Krunal