[swift] 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?

This question is related to swift xcode terminal

The answer is


You will get this under the project setting

enter image description here


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


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 :)


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

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


hi frind code type in terminal swift -v

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


Open the Terminal and write:

swift -version

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:~ system$ swift
Welcome to Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7).
Type :help for assistance.

/usr/bin/swiftc --version

and swift version <--> Xcode version


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

  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


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.


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)


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


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 :]


Examples related to swift

Make a VStack fill the width of the screen in SwiftUI Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code in Xcode 10 Convert Json string to Json object in Swift 4 iOS Swift - Get the Current Local Time and Date Timestamp Xcode 9 Swift Language Version (SWIFT_VERSION) How do I use Safe Area Layout programmatically? How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator Safe Area of Xcode 9 The use of Swift 3 @objc inference in Swift 4 mode is deprecated?

Examples related to xcode

Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64 iPhone is not available. Please reconnect the device Make a VStack fill the width of the screen in SwiftUI error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65 The iOS Simulator deployment targets is set to 7.0, but the range of supported deployment target version for this platform is 8.0 to 12.1 Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Xcode 10: A valid provisioning profile for this executable was not found Xcode 10, Command CodeSign failed with a nonzero exit code

Examples related to terminal

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave Flutter command not found VSCode Change Default Terminal How to switch Python versions in Terminal? How to open the terminal in Atom? Color theme for VS Code integrated terminal How to edit a text file in my terminal How to open google chrome from terminal? Switch between python 2.7 and python 3.5 on Mac OS X