我如何看到我正在使用的Swift版本?

时间:2023-02-01 07:17:02

I just created a new Swift project within Xcode. I am wondering which version of Swift it's using.

我刚刚在Xcode中创建了一个新的Swift项目。我想知道它使用的是哪个版本的Swift。

How can I see, in Xcode or the terminal, what version of Swift I am using inside my project?

如何在Xcode或终端中看到我在项目中使用的Swift版本?

8 个解决方案

#1


124  

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.

项目构建设置有一个块“Swift编译器-语言”,它以键值格式存储关于Swift语言版本的信息。它将显示所有可用的(支持的)Swift语言版本的Xcode和活动版本的一个标记。

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.

项目►(选择您的项目目标)►►构建设置(在搜索栏中输入“swift_version”)迅速编译器语言迅速►►语言版本点击语言列表来打开它。

Look at this snapshot, for easy understanding:

请看这张快照,为了便于理解:

我如何看到我正在使用的Swift版本?


With help of following code, programmatically you can find Swift version supported by your project.

在以下代码的帮助下,您可以通过编程方式找到项目支持的Swift版本。

#if 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.0)
print("Hello, Swift 3.0")
#elseif swift(>=2.2)
print("Hello, Swift 2.2")
#elseif swift(>=2.1)
print("Hello, Swift 2.1")
#endif

Here is result using Playground (with Xcode 9.4)

这里是使用游乐场的结果(Xcode 9.4)

我如何看到我正在使用的Swift版本?

#2


443  

What I do is say in the Terminal:

我所做的就是在终端里说:

$ xcrun swift -version

Output for Xcode 6.3.2 is:

Xcode 6.3.2的输出为:

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正确地指向您的Xcode副本。如果您像我一样,同时处理多个版本的Xcode,那将是一个令人担忧的问题!确保它是,比如说。

$ xcrun --find swift

and look at the path to Xcode that it shows you. For example:

看看它显示的Xcode路径。例如:

/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.

如果这是您的Xcode,那么-version的输出是准确的。如果需要重新指向xcrun,请使用Xcode的location首选项窗格中的命令行工具弹出菜单。

#3


78  

Open the Terminal and write:

打开终端,写:

swift -version

#4


58  

From Xcode 8.3 onward Build Settings has key Swift Language Version with a value of swift version your target is using.

从Xcode 8.3开始,构建设置有关键的Swift语言版本,其值是您的目标正在使用的Swift版本。

For older Xcodes use this solution, open terminal and type following command(s)

对于旧的xcode,使用此解决方案,打开终端,输入以下命令

Case 1: You have installed only one Xcode App

案例1:您只安装了一个Xcode应用程序

swift -version

Case 2: You have installed multiple Xcode Apps

案例2:您安装了多个Xcode应用程序

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

    切换active developer目录(替换Xcode_7.3)。应用程序从您想要查看swift版本的应用程序目录中使用您的Xcode应用程序文件名

     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.

注意:从Xcode 8到Xcode 8.2。x您可以使用swift 2.3,即使Xcode 8使用swift 3。作为默认的swift版本。要使用swift 2.3,只需打开标志,使用遗留的swift语言版本,从构建设置到YES, XCode将使用swift 2.3作为项目目标。

#5


17  

This reddit post helped me: https://www.reddit.com/r/swift/comments/4o8atc/xcode_8_which_swift/d4anpet

这篇reddit上的文章帮助了我:https://www.reddit.com/r/swift/comments/4o8atc/xcode_8_who_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.

Xcode 8默认使用Swift 3.0。但是你可以打开Swift 2.3。转到项目的构建设置,将“使用遗留的Swift语言版本”设置为YES。

Good old reddit :)

好老reddit:)

#6


16  

You can see and select which Swift version Xcode is using in:

您可以在以下文件中看到并选择使用哪个Swift版本的Xcode:

Target -> Build Settings -> Swift Language Version:

目标->构建设置-> Swift语言版本:

我如何看到我正在使用的Swift版本?

This is available in Xcode 8.3 and Xcode 9 (haven't checked older versions)

这在Xcode 8.3和Xcode 9中都有(没有检查旧版本)

#7


0  

To see the default version of swift installed on your machine then from the command line, type the following :

要查看安装在您的机器上的默认swift版本,请在命令行中输入以下内容:

swift --version

Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

苹果Swift版本4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

Target: x86_64-apple-darwin17.6.0

目标: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).

这很可能是您已经安装的应用程序商店版Xcode中包含的版本(除非您已经修改过它)。

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

如果您想要确定一个特定版本的Xcode(例如,一个测试版)使用的Swift的实际版本,那么从命令行中调用Swift二进制文件,并将其传递给它。

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

苹果Swift版本4.2 (swiftlang-1000.0.16.7 clang-1000.10.25.3)

Target: x86_64-apple-darwin17.6.0

目标:x86_64-apple-darwin17.6.0

#8


-2  

Project ► (Select Your Project Target) ► Build Settings ► (Type 'swift' in Searchbar) Swift Compiler Language ► Swift Language Version ► Click on Language list to open it.

项目►(选择您的项目目标)►►构建设置(Searchbar类型“迅速”)迅速编译器语言迅速►►语言版本点击语言列表来打开它。

#1


124  

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.

项目构建设置有一个块“Swift编译器-语言”,它以键值格式存储关于Swift语言版本的信息。它将显示所有可用的(支持的)Swift语言版本的Xcode和活动版本的一个标记。

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.

项目►(选择您的项目目标)►►构建设置(在搜索栏中输入“swift_version”)迅速编译器语言迅速►►语言版本点击语言列表来打开它。

Look at this snapshot, for easy understanding:

请看这张快照,为了便于理解:

我如何看到我正在使用的Swift版本?


With help of following code, programmatically you can find Swift version supported by your project.

在以下代码的帮助下,您可以通过编程方式找到项目支持的Swift版本。

#if 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.0)
print("Hello, Swift 3.0")
#elseif swift(>=2.2)
print("Hello, Swift 2.2")
#elseif swift(>=2.1)
print("Hello, Swift 2.1")
#endif

Here is result using Playground (with Xcode 9.4)

这里是使用游乐场的结果(Xcode 9.4)

我如何看到我正在使用的Swift版本?

#2


443  

What I do is say in the Terminal:

我所做的就是在终端里说:

$ xcrun swift -version

Output for Xcode 6.3.2 is:

Xcode 6.3.2的输出为:

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正确地指向您的Xcode副本。如果您像我一样,同时处理多个版本的Xcode,那将是一个令人担忧的问题!确保它是,比如说。

$ xcrun --find swift

and look at the path to Xcode that it shows you. For example:

看看它显示的Xcode路径。例如:

/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.

如果这是您的Xcode,那么-version的输出是准确的。如果需要重新指向xcrun,请使用Xcode的location首选项窗格中的命令行工具弹出菜单。

#3


78  

Open the Terminal and write:

打开终端,写:

swift -version

#4


58  

From Xcode 8.3 onward Build Settings has key Swift Language Version with a value of swift version your target is using.

从Xcode 8.3开始,构建设置有关键的Swift语言版本,其值是您的目标正在使用的Swift版本。

For older Xcodes use this solution, open terminal and type following command(s)

对于旧的xcode,使用此解决方案,打开终端,输入以下命令

Case 1: You have installed only one Xcode App

案例1:您只安装了一个Xcode应用程序

swift -version

Case 2: You have installed multiple Xcode Apps

案例2:您安装了多个Xcode应用程序

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

    切换active developer目录(替换Xcode_7.3)。应用程序从您想要查看swift版本的应用程序目录中使用您的Xcode应用程序文件名

     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.

注意:从Xcode 8到Xcode 8.2。x您可以使用swift 2.3,即使Xcode 8使用swift 3。作为默认的swift版本。要使用swift 2.3,只需打开标志,使用遗留的swift语言版本,从构建设置到YES, XCode将使用swift 2.3作为项目目标。

#5


17  

This reddit post helped me: https://www.reddit.com/r/swift/comments/4o8atc/xcode_8_which_swift/d4anpet

这篇reddit上的文章帮助了我:https://www.reddit.com/r/swift/comments/4o8atc/xcode_8_who_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.

Xcode 8默认使用Swift 3.0。但是你可以打开Swift 2.3。转到项目的构建设置,将“使用遗留的Swift语言版本”设置为YES。

Good old reddit :)

好老reddit:)

#6


16  

You can see and select which Swift version Xcode is using in:

您可以在以下文件中看到并选择使用哪个Swift版本的Xcode:

Target -> Build Settings -> Swift Language Version:

目标->构建设置-> Swift语言版本:

我如何看到我正在使用的Swift版本?

This is available in Xcode 8.3 and Xcode 9 (haven't checked older versions)

这在Xcode 8.3和Xcode 9中都有(没有检查旧版本)

#7


0  

To see the default version of swift installed on your machine then from the command line, type the following :

要查看安装在您的机器上的默认swift版本,请在命令行中输入以下内容:

swift --version

Apple Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

苹果Swift版本4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)

Target: x86_64-apple-darwin17.6.0

目标: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).

这很可能是您已经安装的应用程序商店版Xcode中包含的版本(除非您已经修改过它)。

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

如果您想要确定一个特定版本的Xcode(例如,一个测试版)使用的Swift的实际版本,那么从命令行中调用Swift二进制文件,并将其传递给它。

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

苹果Swift版本4.2 (swiftlang-1000.0.16.7 clang-1000.10.25.3)

Target: x86_64-apple-darwin17.6.0

目标:x86_64-apple-darwin17.6.0

#8


-2  

Project ► (Select Your Project Target) ► Build Settings ► (Type 'swift' in Searchbar) Swift Compiler Language ► Swift Language Version ► Click on Language list to open it.

项目►(选择您的项目目标)►►构建设置(Searchbar类型“迅速”)迅速编译器语言迅速►►语言版本点击语言列表来打开它。