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

19

744

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?

Openminded answered 11/6, 2015 at 19:52 Comment(1)
Besides the title of #29140976 I find no similarity to this question that's enough to label this question as it's duplicate. The essence of this question is not how to find the version of Swift programmatically, but how to find the version of Swift in a general way (via terminal or Xcode or etc.). This is a big difference.Openminded
S
583

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.9)
print("Hello, Swift 5.9")

#elseif swift(>=5.8)
print("Hello, Swift 5.8")

#elseif swift(>=5.7)
print("Hello, Swift 5.7")

#elseif swift(>=5.6)
print("Hello, Swift 5.6")

#elseif swift(>=5.5)
print("Hello, Swift 5.5")

#elseif swift(>=5.4)
print("Hello, Swift 5.4")

#elseif 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

Syneresis answered 6/9, 2017 at 17:10 Comment(17)
I think this is correct answer. Easier than calling terminal. (especially if you have different version of Xcode installed)Kathie
This is the correct answer because swift version is configured on a per target basis.Village
I don't see this propoerty in my project with Xcode 8.1Panpipe
you can see this property on latest versions of xcodeNewton
If you are using cocoa pods and downgrade swift this way you might have to reinstall your pods for them to compile properly with the new swift version.Ay
It the value is Unspecified, how can you tell what you're compiling against?Birdwatcher
I could see this in Xcode 10.0. In 10.1, I can't find it anymore. Where did it go?Minotaur
@JonMcClung - The same option is availble in Xcode 10.1 also. I just checked it.Syneresis
@Krunal, yeah my bad. I was checking on the wrong project -- it was Objective-C based and didn't have any Swift code, so that's probably why.Minotaur
What about when it says 'unspecified'?Surfboarding
@Surfboarding - Can you please share your code, so we can check the issue. Almost we have covered all the versions of Swift there may not be such scope. Either you may be trying with Objective-C project.Syneresis
I'm using this approach to make a table of which versions of Swift are supported by each version of Xcode: https://mcmap.net/q/55547/-what-versions-of-swift-are-supported-by-what-versions-of-xcodeCharlinecharlock
When I look in my project's build settings it shows 'Swift 5'. If I open a terminal and type 'swift -version' it returns 'Swift version 5.1.3' so using the terminal is giving more detailed information. I'm using Xcode version 11.3.1Deletion
For newbies like me, when XCode 11 looks a little different, note that you simply need to click on the project icon at the top of the file hierarchy on the left sidebar to view this Project UI, the blueprint icon with your project name above the folder containing your swift files. Then this info is easily accessible under Swift Compiler - Language (in Swift Language Version) in the Build Settings tab.Ultann
The max version is not precise there xcrun swift -version is good oneSternberg
This is helpful for editors and environments other than Xcode!Robillard
As of Oct 2, 2023...Xcode 14.3 prints Swift 5.8, Xcode 15.0 prints Swift 5.9. In case this helps anyone.Muskogee
E
630

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.

Eurythermal answered 11/6, 2015 at 20:13 Comment(5)
You can also use xcode-select -p to print the path to the Xcode that xcrun will use, and sudo xcode-select -s /path/to/Xcode.app to change it.Idiolect
I just started to learn ios development and I am surprised that xcode does not let you choose the version of swift nor even let you know the version from GUI.Simarouba
@RadekWilczak I don't know what you mean by "should be". I just copied the posted command and pasted into Terminal and hit Return, and it works. So maybe there's another alternative, but what I said isn't wrong.Eurythermal
This gave me the wrong answer. Because swift is configured per target. See the answer below from @Krunal for the best answer.Village
I agree with all the "the correct way is to set the SWIFT_VERSION" per project. However, in earlier Xcode versions like 8.2, it doesn't state clearly in the UI what version of XCode is supported in the project, and just has "Use Legacy Swift Language Version" in the UI with Yes, No, unspecified. The command line approach is extremely useful to be able to quickly query the answer to what is the max swift version available.Szabadka
S
583

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.9)
print("Hello, Swift 5.9")

#elseif swift(>=5.8)
print("Hello, Swift 5.8")

#elseif swift(>=5.7)
print("Hello, Swift 5.7")

#elseif swift(>=5.6)
print("Hello, Swift 5.6")

#elseif swift(>=5.5)
print("Hello, Swift 5.5")

#elseif swift(>=5.4)
print("Hello, Swift 5.4")

#elseif 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

Syneresis answered 6/9, 2017 at 17:10 Comment(17)
I think this is correct answer. Easier than calling terminal. (especially if you have different version of Xcode installed)Kathie
This is the correct answer because swift version is configured on a per target basis.Village
I don't see this propoerty in my project with Xcode 8.1Panpipe
you can see this property on latest versions of xcodeNewton
If you are using cocoa pods and downgrade swift this way you might have to reinstall your pods for them to compile properly with the new swift version.Ay
It the value is Unspecified, how can you tell what you're compiling against?Birdwatcher
I could see this in Xcode 10.0. In 10.1, I can't find it anymore. Where did it go?Minotaur
@JonMcClung - The same option is availble in Xcode 10.1 also. I just checked it.Syneresis
@Krunal, yeah my bad. I was checking on the wrong project -- it was Objective-C based and didn't have any Swift code, so that's probably why.Minotaur
What about when it says 'unspecified'?Surfboarding
@Surfboarding - Can you please share your code, so we can check the issue. Almost we have covered all the versions of Swift there may not be such scope. Either you may be trying with Objective-C project.Syneresis
I'm using this approach to make a table of which versions of Swift are supported by each version of Xcode: https://mcmap.net/q/55547/-what-versions-of-swift-are-supported-by-what-versions-of-xcodeCharlinecharlock
When I look in my project's build settings it shows 'Swift 5'. If I open a terminal and type 'swift -version' it returns 'Swift version 5.1.3' so using the terminal is giving more detailed information. I'm using Xcode version 11.3.1Deletion
For newbies like me, when XCode 11 looks a little different, note that you simply need to click on the project icon at the top of the file hierarchy on the left sidebar to view this Project UI, the blueprint icon with your project name above the folder containing your swift files. Then this info is easily accessible under Swift Compiler - Language (in Swift Language Version) in the Build Settings tab.Ultann
The max version is not precise there xcrun swift -version is good oneSternberg
This is helpful for editors and environments other than Xcode!Robillard
As of Oct 2, 2023...Xcode 14.3 prints Swift 5.8, Xcode 15.0 prints Swift 5.9. In case this helps anyone.Muskogee
T
135

Open the Terminal and write:

swift -version
Tannate answered 2/6, 2016 at 18:44 Comment(1)
This is not necessarily the version of swift that Xcode sees. Besides, you can have swift without having Xcode.Lumumba
A
73

From Xcode 8.3 onward Build Settings has key Swift Language Version with a value of swift version your target is using.

For older Xcodes use this solution, open terminal and type following command(s)

Case 1: You have installed only one Xcode App

swift -version

Case 2: You have installed multiple Xcode Apps

  • Switch active developer directory (Replace Xcode_7.3.app from following command with your Xcode app file name from Application directory for which you want to check swift version)

     sudo xcode-select --switch /Applications/Xcode_7.3.app/Contents/Developer
    
  • Then

     swift -version
    

NOTE: From Xcode 8 to Xcode 8.2.x you can use swift 2.3 even though Xcode 8 uses swift 3.x as default swift version. To use swift 2.3, just turn on flag Use Legacy Swift Language Version to YES from Build Setting and XCode will use Swift 2.3 for that project target.

Austerity answered 18/7, 2016 at 20:49 Comment(0)
I
26

You can see and select which Swift version Xcode is using in:

Target -> Build Settings -> Swift Language Version:

enter image description here

This is available in Xcode 8.3 and Xcode 9 (haven't checked older versions)

Intermezzo answered 9/6, 2017 at 15:38 Comment(3)
Swift Language Version doesn't come up as a setting in my Xcode 8.2.1 project. It does have Use Legacy Swift Language Version.Formaldehyde
@ChrisPrince Yes, Swift Language Version didn't exist here in Build Settings until Xcode 8.3.... in prior Xcode 8.x versions Use Legacy Swift Language Version, No = Swift 3, and Yes = Swift 2.3Territoriality
I only see the major version not the minor / patch components. e.g. right now I only see "Swift 5"Nameplate
C
24

To see the default version of swift installed on your machine then from the command line, type the following :

swift --version

Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

Target: x86_64-apple-darwin17.6.0

This is most likely the version that is included in the app store version of Xcode that you have installed (unless you have changed it).

If you want to determine the actual version of Swift being used by a particular version of Xcode (a beta, for instance) then from the command line, invoke the swift binary within the Xcode bundle and pass it the parameter --version

/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift --version

Apple Swift version 4.2 (swiftlang-1000.0.16.7 clang-1000.10.25.3)

Target: x86_64-apple-darwin17.6.0

Cootch answered 12/6, 2018 at 15:47 Comment(0)
E
22

In case anyone is looking for quick one-to-one mapping of Swift version based on Xcode Version:

Xcode 13.4.1 :      Swift version 5.6.1

Xcode 13.2   :      Swift version 5.5.2

Xcode 12.5   :      Swift version 5.4.2

Xcode 12.3   :      Swift version 5.3.2

Xcode 12.2   :      Swift version 5.3.1

Xcode 11.6   :      Swift version 5.2.4

Xcode 11.5   :      Swift version 5.2.4

Xcode 11.4   :      Swift version 5.2

Xcode 11.3   :      Swift version 5.1.3

Xcode 11.2.1 :      Swift version 5.1.2

Xcode 11.1   :      Swift version 5.1

Obtained with running following command as mentioned on different Xcode versions:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift --version
Error answered 12/6, 2020 at 10:51 Comment(0)
P
19

This reddit post helped me: https://www.reddit.com/r/swift/comments/4o8atc/xcode_8_which_swift/d4anpet

Xcode 8 uses Swift 3.0 as default. But you can turn on Swift 2.3. Go to project's Build Settings and set 'Use Legacy Swift Language Version' to YES.

Good old reddit :)

Pusher answered 14/10, 2016 at 2:38 Comment(2)
I can't find this by search 'Use Legacy Swift Language Version' or 'Legacy'Mauromaurois
I'm using latest Xcode Version 8.3.2 (8E2002) and swift 3 there in Swift Language Version. Now I want to change this to swift 2.3 but there is only one option swift 3.0 and 2nd is unspecified. so would you please guide how this would be possible ? to get swift 2.3 in my current project? @PusherUrrutia
J
10
/usr/bin/swiftc --version

and swift version <--> Xcode version

Janayjanaya answered 18/4, 2019 at 3:57 Comment(0)
P
8

Either you can run a command on terminal

xcrun swift -version

or

You can refer below table to check which Xcode is using which version of swift language.

Xcode 13.4.1 :      Swift version 5.6.1

Xcode 13.3   :      Swift version 5.6

Xcode 13.2   :      Swift version 5.5.2

Xcode 12.5   :      Swift version 5.4.2

Xcode 12.3   :      Swift version 5.3.2

Xcode 12.2   :      Swift version 5.3.1

Xcode 11.6   :      Swift version 5.2.4

Xcode 11.5   :      Swift version 5.2.4

Xcode 11.4   :      Swift version 5.2

Xcode 11.3   :      Swift version 5.1.3

Xcode 11.2.1 :      Swift version 5.1.2

Xcode 11.1   :      Swift version 5.1
Pegg answered 4/4, 2022 at 12:39 Comment(1)
Thank you. The terminal command was helpful because the version in my project was set to "Unspecified"Bula
M
3

I am using Swift from Google Colab. Here's how to check it in Colab.

!/swift/toolchain/usr/bin/swift --version

The result is 5.0-dev

Meader answered 14/3, 2019 at 6:21 Comment(0)
O
3

Type in terminal

$ swift -v

Output in terminal

Welcome to Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53).

Olmos answered 21/12, 2020 at 14:23 Comment(2)
short and sweetFoliose
The help for -v says "Show commands to run and use verbose output" so this isn't really what OP is asking for even if the command includes a printout of the current versionAdige
P
2

if you want to check the run code for a particular version of swift you can use

#if compiler(>=5.1) //4.2, 3.0, 2.0 replace whatever swft version you wants to check
#endif
Pedo answered 8/1, 2021 at 10:6 Comment(0)
T
1

Updated answer for how to find which version of Swift your project is using in a few click in Xcode 12 to help out rookies like me.

  1. Click on your Project (top level Blue Icon in the left hand pane)
  2. Click on Build Settings (5th item in the Project > Header)
  3. Scroll down to Swift Compiler - Language, and look at the dropdown.

enter image description here

Telephoto answered 20/9, 2020 at 3:38 Comment(0)
E
1

I was able to find the version of the swift from the terminal by the following command:

swift -version. You can also type swift --help to see more commands you can use with swift language on the terminal.

Another way to find and make sure that XCode is indeed using that same version of swift is to go to the project's build setting and check the Swift Language version under the Swift Compiler - Language section. Refer screenshot below:

enter image description here

Experimental answered 18/7, 2023 at 7:17 Comment(1)
The tip in the first part is fantastic TYFoliose
P
-1

Typing

$ swift -v

on the terminal would give you the version in this format

swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
Target: x86_64-apple-macosx12.0

And if you want to see the version on the Xcode, then you can refer to below the screenshot

enter image description here

Praetorian answered 2/4 at 17:47 Comment(1)
The Xcode part of the answer is wrong (as is every answer on the page which mentions that). That value in Xcode is >>> what the app you have open needs <<<. It has no connection at all to "what version of Swift is on the machine".Foliose
R
-2

By just entering swift command in the terminal, it will show the version, while logging to Swift console.(something like below)

System-IOSs-MacBook-Air swift
Welcome to Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7).
Type :help for assistance.
Rm answered 27/9, 2019 at 12:34 Comment(2)
just FTR it no longer does that when you type "swift" per seFoliose
it does not work with the swift commandPraetorian
U
-3

Bonus contribution: I'm using a custom node.js script to extract a clean string for use with Jazzy documentation. You might get some use of this if you can find a place to work it into your dev process:

Invoked from a Bash script:

#!/bin/bash
swiftversion=$(node SwiftVerSlicer.js "${xcrun swift -version}");
echo $swiftversion

SwiftVerSlicer.js:

// begin script
const inputString = `${process.argv[2]}`
let searchTerm = (inputString.indexOf('(') - 1)//-1 cause whitespace
let version = inputString.slice(0,searchTerm)
console.log(version)
// end script

You can also use regex of course, but do whatever you like :]

Undershorts answered 9/2, 2019 at 5:19 Comment(0)
E
-4
  1. Select your project
  2. Build Setting
  3. search for "swift language"
  4. now you can see which swift version you are using in your project

https://i.stack.imgur.com/Ojn3m.png

Excrescency answered 12/11, 2019 at 9:13 Comment(2)
How is that different from the first solution given in https://mcmap.net/q/54072/-how-do-i-see-which-version-of-swift-i-39-m-using?Exhilarant
well i am going throw main project directory and when you type "swift language" it is only show swift version on top in Build Settings nothing other than that..Excrescency

© 2022 - 2024 — McMap. All rights reserved.