Have you ever wondered what version of Swift you are using in the project you're working on? The answer is less straightforward that you might think. This episode of Swift Fundamentals sheds some light on this seemingly simple question.
Using Xcode
Fire up Xcode and create a project using the App template from the iOS > Application section.
Configure the project and tell Xcode where you would like to save the project files on your machine.
To figure out which version of Swift the project uses, we take a look at the build settings of the target. Click the project in the Project Navigator and inspect the build settings of the target by selecting the Build Settings tab at the top.
Enter Swift in the search field in the top right. One of the build settings of the target is named Swift Language Version. I'm using Xcode 14 so the Swift version for a new project is Swift 5.
You can switch to a different version of the Swift language using the dropdown menu. Xcode 14 supports Swift 4, 4.2, and 5.
Using the Command Line
The problem is that Xcode only shows the major version or, in the case of Swift 4.2, the major and the minor version. Let's use the command line to find out what Swift version Xcode ships with. Open a terminal window. We use the xcrun
command first. Execute the following command.
xcrun swift --version
Because I'm using Xcode 14, the output of the above command shows that Xcode 14 ships with Swift 5.7.
swift-driver version: 1.62.3 Apple Swift version 5.7 (swiftlang-5.7.0.123.8 clang-1400.0.29.50)
Target: arm64-apple-macosx12.0
I usually have multiple versions of Xcode installed. Let's switch to Xcode 13.4.1 using the xcode-select
command.
sudo xcode-select -s /Applications/Xcode.app
Let's run the xcrun swift --version
command one more time to illustrate the difference. Even though both Xcode 13.4.1 and Xcode 14 ship with Swift 5, the versions are different.
swift-driver version: 1.45.2 Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
Target: arm64-apple-macosx12.0
This may seem unimportant and it is less of an issue compared to the early days of Swift. It does matter, though. Every Swift release introduces changes, features, and bug fixes.
Which Swift
Let's dig a bit deeper to better understand what happens under the hood. Open a terminal window and execute the following command.
which swift
This command points you to the location of the REPL (Read Eval Print Loop) for Swift on your machine. It should be located at /usr/bin/swift
. If you now execute the swift --version
command, you should see the same output we saw when we executed the xcrun swift --version
command.
swift-driver version: 1.45.2 Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
Target: arm64-apple-macosx12.0
Let's use xcode-select
one more time to switch to Xcode 14 and execute the which swift
command. The output still points to /usr/bin/swift
even though the version changed. How is that possible?
swift-driver version: 1.62.3 Apple Swift version 5.7 (swiftlang-5.7.0.123.8 clang-1400.0.29.50)
Target: arm64-apple-macosx12.0
The interactive environment for Swift located at /usr/bin/swift
is nothing more than a stub that forwards commands to the active toolchain, Xcode 14 in this example. You can find its location using the xcrun
command.
xcrun --find swift
The output shows that it is located deep within the Xcode package.
/Applications/Xcode 14/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
Using Code
You can also use Swift to find out what version of the language you're using. Create a playground in Xcode and add the following snippet.
import Foundation
#if swift(>=5.7)
print("Swift 5.7")
#elseif swift(>=5.6)
print("Swift 5.6")
#elseif swift(<5.6)
print("Lower than Swift 5.6")
#endif
Executing the contents of the playground gives the following output using Xcode 14.
Swift 5.7
This method is coarse since you can only use the >=
and <
operators. While I have never had a need for this solution, it can be useful if you're building a library that is used by third parties. It allows you to use features of later Swift versions in your library without breaking it for developers that are using an older version of the language.
What's Next?
Knowing which Swift version you are using can be useful to know which features of the language are available to you. In the early days of the language, major versions of the language were released on an almost yearly basis. Now that the language is mature and stable, minor and patch releases have become the norm. The next major release of the language, Swift 6, is coming, but it is unclear when at this point.