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?
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?
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:
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)
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.
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.
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 :)
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
In case anyone is looking for quick one-to-one mapping of Swift version based on Xcode Version:
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
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.
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 :]