Xcode comes bundled with a version of Swift. You can refer to this list for the shipped versions. Sometimes, you won’t want to upgrade Swift for your project. So if you update Xcode, yet you need an older Swift version, this is the guide for you.
Previously, you can select the toolchain under Xcode > Preferences > Components > Toolchains, but it seems like as of 2022, this feature has been removed. UPDATE: After you have installed another toolchain, the options will appear.. Select in Xcode > Preferences > Components > Toolchains.
Check your swift
Firstly, know the version.
xcrun swift --version
Eg. swift-driver version: 1.45.2 Apple Swift version 5.6 (swiftlang-5.6.0.323.62 clang-1316.0.20.8)
, Target: arm64-apple-macosx12.0
But where is the swift toolchain from?
xcrun -f swift
Eg. /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
This is the default Xcode app if installed from Mac App Store.
Installing alternative swift toolchain
You can download another swift version (select Toolchain for Xcode), and install it.
Thereafter, you can find it installed in la /Library/Developer/Toolchains
.
We need to know the identifier for the toolchain to use. To do so, you can run eg.
cat /Library/Developer/Toolchains/swift-5.5-RELEASE.xctoolchain/Info.plist
Look for CFBundleIdentifier
eg. org.swift.550202109201a
With the identifier, you can now specify the toolchain to use:
xcodebuild -toolchain org.swift.550202109201a
xcrun -toolchain org.swift.550202109201a
Or export as an environment variable:
export TOOLCHAINS=org.swift.550202109201a
With that, you can check your swift version with xcrun swift --version
and xcrun -f swift
, and it should be the toolchain you have just specified.