-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swift 5.4 的新特性 #8123
Swift 5.4 的新特性 #8123
Conversation
Merge New Resource
# Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
# Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit.
@@ -109,7 +111,7 @@ We had to use the `buildBlock` method directly when defining a value. Therefore, | |||
print(makeSentence()) | |||
``` | |||
|
|||
What we have done with result builders so far may not mean much for you, but if we use result builders a little more effectively, you will understand their power better. For example, with two new methods that we will add to our result builder, we can use conditions to generate `String` elements using our result builder. The code is as follows: | |||
目前为止,我们用 Result 构建器所作的工作对你来说可能没有什么意义。但如果我们更有效地使用 Result 构建器,你将更好地理解它们的威力。例如,有了这两个将添加到 Result 构建器中的新方法,我们可以使用 Result 构建器来有条件的生成字符串。代码如下: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前为止,我们用 Result 构建器所作的工作对你来说可能没有什么意义。但如果我们更有效地使用 Result 构建器,你将更好地理解它们的威力。例如,有了这两个将添加到 Result 构建器中的新方法,我们可以使用 Result 构建器来有条件的生成字符串。代码如下: | |
目前为止,我们用 Result 构建器所作的工作对你来说可能没有什么意义。但如果我们更有效地使用 Result 构建器,你将更好地理解它们的威力。例如,有了这两个将添加到 Result 构建器中的新方法,我们可以使用 Result 构建器来有条件的生成字符串。代码如下: |
|
||
```Swift | ||
func method(singleVariadicParameter: String) {} | ||
``` | ||
|
||
Now, we can write multiple variadic parameters like the code below: | ||
现在,我们可以像下面的代码那样编写多个可变参数: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现在,我们可以像下面的代码那样编写多个可变参数: | |
现在,我们可以像下面的代码那样编写多个可变参数: |
|
||
As anyone who has created an Xcode project or playground file before will know, when you create a new playground or a new Xcode project, the following value will be written on this project: | ||
正如任何之前创建过 Xcode 项目或 playground 文件的人所知道的一样,当你创建一个新的 playground 或 Xcode 项目时,下面的值会被写入到这个项目中: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
正如任何之前创建过 Xcode 项目或 playground 文件的人所知道的一样,当你创建一个新的 playground 或 Xcode 项目时,下面的值会被写入到这个项目中: | |
正如任何之前创建过 Xcode 项目或 playground 文件的人所知道的一样,当你创建一个新的 playground 或 Xcode 项目时,下面的值会被写入到这个项目中: |
校对认领 |
@PassionPenguin 欢迎校对 |
# What’s New in Swift 5.4? | ||
# Swift 5.4 的新特性 | ||
|
||
多个可变参数,拓展的隐式成员语法,Result 构建器,等等... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
多个可变参数,拓展的隐式成员语法,Result 构建器,等等... | |
> 多个可变参数,拓展的隐式成员语法,Result 构建器,等等…… |
# What’s New in Swift 5.4? | ||
# Swift 5.4 的新特性 | ||
|
||
多个可变参数,拓展的隐式成员语法,Result 构建器,等等... | ||
|
||
![Photo by the author.](https://cdn-images-1.medium.com/max/3840/1*HfwBHnUJOzl56qCflMVQ1w.png) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
![Photo by the author.](https://cdn-images-1.medium.com/max/3840/1*HfwBHnUJOzl56qCflMVQ1w.png) | |
![图源作者](https://cdn-images-1.medium.com/max/3840/1*HfwBHnUJOzl56qCflMVQ1w.png) |
|
||
![Photo by the author.](https://cdn-images-1.medium.com/max/3840/1*HfwBHnUJOzl56qCflMVQ1w.png) | ||
|
||
Swift 5.4 brings us a lot which is why I like it. In this article, we learn what is new in Swift 5.4. | ||
Swift 5.4 带来了许多改变,这也是我喜欢它的原因。在本文中,我们将学习 Swift 5.4 的新特性。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swift 5.4 带来了许多改变,这也是我喜欢它的原因。在本文中,我们将学习 Swift 5.4 的新特性。 | |
Swift 5.4 带来了许多改变,而这也是我喜欢它的原因。在本文中,我们将了解 Swift 5.4 的新特性。 |
|
||
> Note: You can [download this article’s example project and sources](https://github.com/Unobliging/What-s-New-in-Swift-5.4-) on GitHub. To open and edit these files, you have to use Xcode 12.5 beta. You can download Xcode 12.5 beta [here](https://developer.apple.com/download/). Instead of downloading Xcode 12.5 beta, you can download Swift 5.4 directly [here](https://swift.org/download/). | ||
> 注意:你可以在 GitHub 上下载 [本文的示例项目和源代码](https://github.com/Unobliging/What-s-New-in-Swift-5.4-) 。要打开和编辑这些文件,你需要使用 Xcode 12.5 beta 版。你可以下载 [Xcode 12.5 beta 版]((https://developer.apple.com/download/))。你也可以直接下载 [Swift 5.4]((https://swift.org/download/)) 而不是下载 Xcode 12.5 beta 版本。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
> 注意:你可以在 GitHub 上下载 [本文的示例项目和源代码](https://github.com/Unobliging/What-s-New-in-Swift-5.4-) 。要打开和编辑这些文件,你需要使用 Xcode 12.5 beta 版。你可以下载 [Xcode 12.5 beta 版]((https://developer.apple.com/download/))。你也可以直接下载 [Swift 5.4]((https://swift.org/download/)) 而不是下载 Xcode 12.5 beta 版本。 | |
> 注意:你可以在 GitHub 上下载 [本文的示例项目和源代码](https://github.com/Unobliging/What-s-New-in-Swift-5.4-)。要打开和编辑这些文件,你需要使用 Xcode 12.5 beta 版或更高级。你可以点击这里下载 [Xcode 12.5 beta 版]((https://developer.apple.com/download/)),或者你也可以选择直接下载 [Swift 5.4]((https://swift.org/download/))。 |
``` | ||
|
||
We can call the function we wrote above, but of course, we can only write one `String` element if we want. Here’s the code: | ||
我们可以这样调用上面所写的函数,同样,如果我们想的话,我们可以只传入一个字符串元素。这是代码: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我们可以这样调用上面所写的函数,同样,如果我们想的话,我们可以只传入一个字符串元素。这是代码: | |
我们可以这样调用上面所写的函数,同样,如果我们想的话,我们可以只传入一个字符串元素。下面的是示例代码: |
|
||
Can we create dozens of strings with a function that outputs a `String`? If we use result builders, the answer is yes! | ||
我们能用一个输出字符串的函数创建几十个字符串吗?如果我们使用 Result 构建器,这个答案是可以用的! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我们能用一个输出字符串的函数创建几十个字符串吗?如果我们使用 Result 构建器,这个答案是可以用的! | |
我们能用一个输出字符串的函数创建几十个字符串吗?如果我们使用 Result 构建器,那么答案是,当然可以! |
|
||
```Swift | ||
.transition(AnyTransistion.scale.move(…)) | ||
``` | ||
|
||
## Functions Support Same Names | ||
## 函数支持相同的名称 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## 函数支持相同的名称 | |
## 支持同名函数 |
|
||
Sometimes, you want to write functions with the same name. At least I wanted to do it. With Swift 5.4, we can do that. | ||
有些,你希望编写同名函数。至少我也是这么希望的。在 Swift 5.4 中,我们可以编写同名函数了。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有些,你希望编写同名函数。至少我也是这么希望的。在 Swift 5.4 中,我们可以编写同名函数了。 | |
有些,你希望编写同名函数 —— 至少我也是这么希望的。在 Swift 5.4 中,我们可以编写同名函数了。 |
|
||
For example, if we create functions with the same names — and these functions have the same parameter name — our code will work if we define these parameters with different object types. | ||
例如,如果我们创建具有相同名称的函数——这些函数具有相同的形参名称——如果我们用不同的对象类型来定义这些形参,我们的代码就会起作用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
例如,如果我们创建具有相同名称的函数——这些函数具有相同的形参名称——如果我们用不同的对象类型来定义这些形参,我们的代码就会起作用。 | |
例如,如果我们创建具有相同名称的函数 —— 这些函数具有相同的形参名称 —— 如果我们用不同的对象类型来定义这些形参,我们的代码就会起作用。 |
记得注意下,两个独立句之间要么用并列关系要么用主从关系嗷 |
@PassionPenguin 已经更新,辛苦校对了 |
@PassionPenguin @lsvih 校对认领。 |
艾特错人啦哈哈哈 |
@flying-yogurt 欢迎校对 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
As anyone who has created an Xcode project or playground file before will know, when you create a new playground or a new Xcode project, the following value will be written on this project: | ||
## 最重要的改进😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
添加额外的空格使其美观。
## 最重要的改进😄 | |
## 最重要的改进 😄 |
|
||
```Swift | ||
var str = "Hello, playground" | ||
``` | ||
|
||
The name of this value has changed with Swift 5.4 as follows: | ||
该值的名称随 Swift 5.4 更改如下: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
调整语序使其通顺。
该值的名称随 Swift 5.4 更改如下: | |
在 Swift 5.4 中,该值的名称更改为: |
|
||
```Swift | ||
var greeting = "Hello, playground" | ||
``` | ||
|
||
Yes, I think this is the interesting and funny part of Swift 5.4. | ||
是的,这就是我认为 Swift 5.4 中最有趣的部分。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这句很难翻译啊……我的个人感觉是作者想在此处添加一些自己的评论,单纯直译为有趣可能会有点生硬?如果觉得不太合适的话保留原翻译即可😁
是的,这就是我认为 Swift 5.4 中最有趣的部分。 | |
这正是我认为 Swift 5.4 中最有趣与有人情味儿的部分。 |
|
||
In Swift 5.4, we can use multiple variadic parameters on functions, methods, subscripts, and initializers. Before Swift 5.4, we had just one variadic parameter, like the code below: | ||
在 Swift 5.4 中,我们可以在函数、方法、下标和初始化器中可以使用多个可变参数。而在此之前,就只能有一个可变参数,就跟下面的代码一样: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 Swift 5.4 中,我们可以在函数、方法、下标和初始化器中可以使用多个可变参数。而在此之前,就只能有一个可变参数,就跟下面的代码一样: | |
在 Swift 5.4 中,我们可以在函数、方法、下标和初始化器中可以使用多个可变参数。而在此之前,就只能有一个可变参数,例如: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个讲解的是可变参数,类似于单个参数
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对的呀。我只是把“就跟下面的代码一样”改成了“例如”,没有改变句子的结构或者意思。因为在这段附近已经用过一次“下面的是示例代码”,为了避免重复,可能观感会好一点。
|
||
Can we create dozens of strings with a function that outputs a `String`? If we use result builders, the answer is yes! | ||
我们能用一个输出字符串的函数创建几十个字符串吗?如果我们使用 Result 构建器,那么答案是,当然可以! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我们能用一个输出字符串的函数创建几十个字符串吗?如果我们使用 Result 构建器,那么答案是,当然可以! | |
我们能用一个输出单个字符串的函数创建几十个字符串吗?假如我们使用 Result 构建器,那么答案是,当然可以! |
@@ -146,31 +148,31 @@ As you can see, by creating an `if` loop, we can change the `String` element acc | |||
print(makeSentence()) | |||
``` | |||
|
|||
There are many things that can be done with result builders. You can find them out by trying. | |||
有很多事情可以通过 Result 构建器来完成。你可以尝试一下,发现它们。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改写使其符合中文行文逻辑。
有很多事情可以通过 Result 构建器来完成。你可以尝试一下,发现它们。 | |
当然啦!你还可以自己尝试使用 Result 构建器来完成许许多多有趣的事情,而不仅仅拘泥于上面的例子。 |
|
||
```Swift | ||
.transition(.scale.move(…)) | ||
``` | ||
|
||
After Swift 5.4, we have to write this code block below for the same result. Here’s the line of code: | ||
在 Swift 5.4 之前,为了得到相同的结果我们必须编写下面的代码。就是下面这行代码: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 Swift 5.4 之前,为了得到相同的结果我们必须编写下面的代码。就是下面这行代码: | |
在 Swift 5.4 发布之前,若想得到相同的结果,我们只能这样写: |
|
||
For example, if we create functions with the same names — and these functions have the same parameter name — our code will work if we define these parameters with different object types. | ||
例如,如果我们创建具有相同名称的函数 —— 这些函数具有相同的形参名称 —— 只要我们用不同的对象类型来定义这些形参,那么我们的代码就会起作用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
例如,如果我们创建具有相同名称的函数 —— 这些函数具有相同的形参名称 —— 只要我们用不同的对象类型来定义这些形参,那么我们的代码就会起作用。 | |
比如说,如果我们创建具有相同名称的函数 —— 这些函数具有相同的形参名称 —— 只要我们用不同的对象类型来定义这些形参,那么我们的代码就会生效。 |
|
||
I hope you found this article helpful. There are new reports that Swift 6.0 may be released. I will also write an article on this subject. | ||
希望这篇文章能对你有帮助。有报道称 Swift 6.0 可能即将发布。到时候我还会写一篇新文章来说明 Swift 6.0。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
希望这篇文章能对你有帮助。有报道称 Swift 6.0 可能即将发布。到时候我还会写一篇新文章来说明 Swift 6.0。 | |
希望这篇文章能对你有帮助。有报道称 Swift 6.0 可能即将发布。到时候,我还会写一篇新文章来介绍 Swift 6.0 的新功能。 |
|
||
--- | ||
|
||
If you want to meet me or have questions about iOS development etc. you can have a one-on-one meeting with me [here](https://superpeer.com/canbalkya). | ||
如果你想和我见面,或者有关于 iOS 开发等方面的问题,你可以在[这里](https://superpeer.com/canbalkya)与我进行一对一的交流。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果你想和我见面,或者有关于 iOS 开发等方面的问题,你可以在[这里](https://superpeer.com/canbalkya)与我进行一对一的交流。 | |
如果你想和我聊聊,或者说你有关于 iOS 开发等方面的问题,我们可以在[这里](https://superpeer.com/canbalkya)进行一对一的交流。 |
谢谢谢谢!啊哈哈哈哈我是不是应该艾特译者哦🥳 |
@flying-yogurt 看一下 |
@LoneyIsError #8123 (comment) 这句我把修改理由放在上面啦。这是我的个人校对意见,如果你觉得原翻译更为妥帖就直接用原来的吧 😊。 |
感谢校对,剩下的几个没有 resolve 的 comment 都属于对文字比较高的要求了,作者可以考虑在发布时参考修改,也可以保持原样,先 merge 了 |
@LoneyIsError 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。 掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件。 |
收到 |
译文翻译完成,resolve #8073