什么是“快速语言版本"? Xcode设置为?因为它仍会使用旧版本集来构建新的Swift代码 [英] What is the "Swift Language Version" Xcode setting for? Because it still builds newer Swift code with an older version set

查看:105
本文介绍了什么是“快速语言版本"? Xcode设置为?因为它仍会使用旧版本集来构建新的Swift代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode项目构建源代码,该项目的"Swift语言版本"设置为"Swift 4".即使有了这个设置,该项目仍会使用新的isMultiple(of:)方法构建Swift 5.1源代码,例如隐式返回和Swift 5源代码.

I'm building source code using an Xcode project that has its "Swift Language Version" set to "Swift 4." Even with this set, the project builds Swift 5.1 source code like, for example, implicit returns and Swift 5 source code using the new isMultiple(of:) method.

即使将"Swift语言版本"设置为"Swift 4"或"Swift 4.2",Swift 5和5.1代码仍然可以使用

The Swift 5 and 5.1 code still works even with "Swift Language Version" set to "Swift 4" or "Swift 4.2"

仅在运行#elseif swift(>=4.1)语句时,看起来才有所不同(来自 https://stackoverflow.com/a的代码/46080904/414415 ).这些结果将输出预期的结果.

It's only when running #elseif swift(>=4.1) statements that there appears to be a difference (code from https://stackoverflow.com/a/46080904/414415). Those results output their expected results.

但是,我不禁要问,当构建设置明确指出Swift 4时,我的Swift 5源代码如何成功编译?而且,如果该设置没有更改我可以使用的Swift版本,那么它实际上是做什么的?

However, I'm left wondering, how does the my Swift 5 source code compile successfully when the build setting clearly states Swift 4? And if the setting does not change which version of Swift I can use, what does it actually do?

推荐答案

在Swift 4之前,编译器的版本和语言是相同的.但是从Swift 4开始,编译器可以在以前的Swift版本的兼容模式下运行.在Swift 4.0发行说明

Prior to Swift 4, the version of the compiler and the language were one and the same. But since Swift 4, the compiler can run in a compatibility mode for previous Swift versions. check more info on compatibility modes in the Swift 4.0 release notes

Xcode构建设置SWIFT_VERSION设置编译器标志-swift-version,这是语言模式.在迅速的编译器中,此参数下方的输出仅会更改输入的解释方式.

The Xcode build setting SWIFT_VERSION set's the compiler flag -swift-version which is the language mode. From the swift compiler print out below this parameter only changes how the input is interpreted.

swiftc -h|grep 'Swift language version number'
  -swift-version <vers>   Interpret input according to a specific Swift language version number

因此,当您选择Swift语言版本为4.2时,这并不意味着使用Swift 4.2编译器.编译器版本仍为5.1.3,"Swift语言版本"设置指示编译器以Swift 4.2兼容模式运行.兼容模式意味着您可能无需修改Swift 4.2代码即可使用新版本的编译器.因为以兼容模式运行的编译器允许Swift 4.2版代码进行编译并与5版及更高版本中的代码一起运行.

Thus When you select Swift Language Version to 4.2, this does not mean use Swift 4.2 compiler. The compiler version will still be 5.1.3, the Swift Language Version setting instructs the compiler to run in Swift 4.2 compatibility mode. The compatibility mode means you may not need to modify your swift 4.2 code to use the new version of the compiler. Because the compiler running in compatibility mode allows Swift version 4.2 code to compile and run alongside code from version 5 and later.

具有兼容模式的Swift 5编译器可以编译使用 Swift 4语法 Swift 4.2语法 Swift编写的代码5种语法.

The Swift 5 compiler with compatibility mode can compile code written with either Swift 4 syntax, Swift 4.2 syntax, or Swift 5 syntax.

这是一个代码示例,使用下面的代码创建文件test.swift:

Here is a code example, create a file test.swift with code below:

//code written before siwft 5
let firstName = "michael jackson"
let offset = firstName.endIndex.encodedOffset

// Check swift version being used.
#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")

#endif

假设上面的代码是在swift 5之前使用swift 4编译器编写的 此代码将编译,没有错误,如下所示.

suppose the above code was written before swift 5 using the swift 4 compiler this code will compile with no error's as shown below.

在swift 5发布之后,如果您尝试使用Swift 5编译器编译此代码,如下所示.

After swift 5 is released if you try to compile this code with Swift 5 compiler as shown below.

由于在迅速5中已弃用encodedOffset,因此您将收到上面显示的警告.

You will get the warning shown above since encodedOffset is deprecated in swift 5.

您可以降级并使用swift 4编译器,也可以在兼容模式下使用带有编译器标志-swift-version的Swift 5编译器,如下所示.

You could downgrade and use the swift 4 compiler or you can use the Swift 5 compiler in compatibility mode with the compiler flag -swift-version as shown below.

请务必注意, Swift 4编译器 Swift-4兼容模式下的是不一样的. swift 5编译器通常会在运行兼容模式时使用新的swift 5语言功能.这使开发人员即使无法升级到Swift 5也可以使用新功能. Swift 4编译器将无法使用新的Swift 5功能.

It's important to note that Swift 4 compiler, and the Swift 5 compiler in Swift-4 compatibility mode are not the same thing. New swift 5 language features are normally available to the swift 5 compiler running compatibility mode. This allows developers to use the new features even when they can't upgrade to swift 5. The new Swift 5 features will not be available to the Swift 4 compiler.

这篇关于什么是“快速语言版本"? Xcode设置为?因为它仍会使用旧版本集来构建新的Swift代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆