How do I know which version of Swift I'm using?

I just created a new Swift project in Xcode. I am wondering which version of Swift is using.

How can I see, in Xcode or terminal, which version of Swift I use in my project?

+505
source share
14 answers

In the project build settings, there is a "Swift Compiler - Languages" block, which stores information about the Swift language version in key-value format. It will show you the entire available (supported) version of the Swift language for your Xcode and the active version also with a checkmark.

► ( ) ► ► ( 'swift_version' ) Swift ► Swift ► , ( ).

, :

enter image description here


Swift, .

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

Playground ( Xcode 11.x)

enter image description here

+327

:

$ xcrun swift -version

Xcode 6.3.2:

Apple Swift version 1.2 (swiftlang-602.0.53.1 clang-602.0.53)

, , xcrun Xcode. , , Xcode, ! , ,

$ xcrun --find swift

Xcode, . :

/Applications/Xcode.app/...

Xcode, -version . xcrun, " " Xcode Locations.

+479

:

swift -version
+97

Xcode 8.3 onward Build Settings Swift Language Version , .

Xcodes , ()

1: Xcode

swift -version

2: Xcode

  • active developer directory ( Xcode_7.3.app Xcode , )

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

.. Xcode 8 Xcode 8.2.x swift 2.3, Xcode 8 swift 3.x . swift 2.3, Use Legacy Swift Language Version YES Build Setting, XCode Swift 2.3 .

+63

, Swift Xcode :

Swift Language:

enter image description here

Xcode 8.3 Xcode 9 ( )

+20

reddit : https://www.reddit.com/r/swift/comments/4o8atc/xcode_8_which_swift/d4anpet

Xcode 8 Swift 3.0 . Swift 2.3. Project Build Settings " Swift" YES.

reddit:)

+18

swift , , :

swift --version

Apple Swift 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

: x86_64-apple-darwin17.6.0

, , Xcode , ( ).

Swift, Xcode (, ), swift Xcode --version

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

Apple Swift 4.2 (swiftlang-1000.0.16.7 clang-1000.10.25.3)

: x86_64-apple-darwin17.6.0

+10

: node.js Jazzy. , :

Bash:

#!/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

, :]

0

Swift Google Colab. Colab.

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

5.0-dev

0
source

Xcode 10.2

img

show image @Krunal

0
source
/usr/bin/swiftc --version

and quick version <-> version of Xcode

0
source

with Swift 5:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    let cell = self.tableView.cellForRow(at: indexPath)
    if cell?.accessoryType == UITableViewCell.AccessoryType.checkmark
    {
        cell?.accessoryType = UITableViewCell.AccessoryType.none
        tableView.deselectRow(at: indexPath, animated: true)
    }else
    {
        cell?.selectionStyle = UITableViewCell.SelectionStyle.none
        cell?.accessoryType = UITableViewCell.AccessoryType.checkmark
    }       
}
0
source

Just by entering the swift command in the terminal, it will show the version and enter the 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.
0
source

Project ► (Select the project goal) ► Build settings ► (enter “swift” in the search bar) Swift language for the compiler ► Fast version of the language ► Click “List of languages” to open it.

-1
source

Source: https://habr.com/ru/post/1694853/


All Articles