1
- <!--
2
- 要翻译的文件:https://github.com/SwiftGGTeam/the-swift-programming-language-in-chinese/blob/swift-6-beta-translation/swift-6-beta.docc/LanguageGuide/Closures.md
3
- Swift 文档源文件地址:https://docs.swift.org/swift-book/documentation/the-swift-programming-language/closures
4
- 翻译估计用时:⭐️⭐️⭐️⭐️⭐️
5
- -->
1
+ # 闭包
2
+
3
+ 对一起执行的代码进行分组,而不创建命名函数。
4
+
5
+ * 闭包* 是自包含的函数代码块,可以在代码中被传递和使用。Swift 中的闭包类似于其他编程语言中的闭包、匿名函数、lambda和块。
6
+
7
+ 闭包可以捕获和存储其所在上下文中任意常量和变量的引用。被称为* 包裹常量* 和变量。 Swift 会为你管理在捕获过程中涉及到的所有内存操作。
8
+
9
+ > Note: 如果你不熟悉捕获(capturing)这个概念也不用担心。
10
+ > 在 < doc:Closures#Capturing-Values > 章节有它更详细的介绍。
11
+
12
+ 在 函数 章节中介绍的全局和嵌套函数实际上也是特殊的闭包,闭包采用如下三种形式之一:
13
+
14
+ - 全局函数是一个有名字但不会捕获任何值的闭包
15
+ - 嵌套函数是一个有名字并可以捕获其封闭函数域内值的闭包
16
+ - 闭包表达式是一个利用轻量级语法所写的可以捕获其上下文中变量或常量值的匿名闭包
17
+
18
+ Swift 的闭包表达式拥有简洁的风格,其优化鼓励在常见场景中使用简短、整洁的语法,主要优化如下:
6
19
7
- # Closures
8
-
9
- Group code that executes together, without creating a named function.
10
-
11
- * Closures* are self-contained blocks of functionality
12
- that can be passed around and used in your code.
13
- Closures in Swift are similar
14
- to closures, anonymous functions, lambdas, and blocks
15
- in other programming languages.
16
-
17
- Closures can capture and store references to any constants and variables
18
- from the context in which they're defined.
19
- This is known as * closing over* those constants and variables.
20
- Swift handles all of the memory management of capturing for you.
21
-
22
- > Note: Don't worry if you aren't familiar with the concept of capturing.
23
- > It's explained in detail below in < doc:Closures#Capturing-Values > .
24
-
25
- Global and nested functions, as introduced in < doc:Functions > ,
26
- are actually special cases of closures.
27
- Closures take one of three forms:
28
-
29
- - Global functions are closures that have a name
30
- and don't capture any values.
31
- - Nested functions are closures that have a name
32
- and can capture values from their enclosing function.
33
- - Closure expressions are unnamed closures written in a lightweight syntax
34
- that can capture values from their surrounding context.
35
-
36
- Swift's closure expressions have a clean, clear style,
37
- with optimizations that encourage brief, clutter-free syntax in common scenarios.
38
- These optimizations include:
39
-
40
- - Inferring parameter and return value types from context
41
- - Implicit returns from single-expression closures
42
- - Shorthand argument names
43
- - Trailing closure syntax
44
-
45
- ## Closure Expressions
46
-
47
- Nested functions, as introduced in < doc:Functions#Nested-Functions > ,
48
- are a convenient means of naming and defining self-contained blocks of code
49
- as part of a larger function.
50
- However, it's sometimes useful to write shorter versions of function-like constructs
51
- without a full declaration and name.
52
- This is particularly true when you work with functions or methods that take functions
53
- as one or more of their arguments.
54
-
55
- * Closure expressions* are a way to write inline closures in a brief, focused syntax.
56
- Closure expressions provide several syntax optimizations
57
- for writing closures in a shortened form without loss of clarity or intent.
58
- The closure expression examples below illustrate these optimizations
59
- by refining a single example of the ` sorted(by:) ` method over several iterations,
60
- each of which expresses the same functionality in a more succinct way.
61
-
62
- ### The Sorted Method
63
-
64
- Swift's standard library provides a method called ` sorted(by:) ` ,
65
- which sorts an array of values of a known type,
66
- based on the output of a sorting closure that you provide.
67
- Once it completes the sorting process,
68
- the ` sorted(by:) ` method returns a new array of the same type and size as the old one,
69
- with its elements in the correct sorted order.
70
- The original array isn't modified by the ` sorted(by:) ` method.
71
-
72
- The closure expression examples below use the ` sorted(by:) ` method
73
- to sort an array of ` String ` values in reverse alphabetical order.
74
- Here's the initial array to be sorted:
20
+ - 利用上下文推断参数和返回值类型
21
+ - 隐式返回单表达式闭包,即单表达式闭包可以省略 return 关键字
22
+ - 参数名称缩写
23
+ - 尾随闭包语法
24
+
25
+ ## 闭包表达式
26
+
27
+ 嵌套函数, 如 < doc:Functions#Nested-Functions > 中介绍的那样,是一种命名和定义自包含代码块作为更大函数的一部分的便捷方法。当然,编写未完整声明和没有函数名的类函数结构代码是很有用的,尤其是在编码中涉及到函数作为一个或多个参数的那些方法时。
28
+
29
+ * 闭包表达式* 是一种以简短、集中的语法编写内联闭包的方法。在保证不丢失它语法清晰和意图的同时,闭包表达式提供了几种优化的语法简写形式。下面的闭包表达式通过对 ` sorted(by:) ` 这一示例的多次迭代来展示这个过程,每次迭代都使用了更加简洁的方式描述了相同功能。
30
+
31
+ ### Sorted 方法
32
+
33
+ Swift 标准库提供了名为 ` sorted(by:) ` 的方法,它会基于你提供的排序闭包表达式的判断结果对数组中的值(类型确定)进行排序。一旦它完成排序过程,` sorted(by:) ` 方法会返回一个与旧数组类型大小相同类型的新数组,该数组的元素有着正确的排序顺序。原数组不会被 ` sorted(by:) ` 方法修改。
34
+
35
+ 下面的闭包表达式示例使用 ` sorted(by:) ` 方法对一个 ` String ` 类型的数组进行字母逆序排序。以下是初始数组:
75
36
76
37
``` swift
77
38
let names = [" Chris" , " Alex" , " Ewa" , " Barry" , " Daniella" ]
@@ -85,19 +46,11 @@ let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
85
46
```
86
47
-->
87
48
88
- The ` sorted(by:) ` method accepts a closure that takes two arguments
89
- of the same type as the array's contents,
90
- and returns a ` Bool ` value to say whether the first value should appear
91
- before or after the second value once the values are sorted.
92
- The sorting closure needs to return ` true `
93
- if the first value should appear * before* the second value,
94
- and ` false ` otherwise.
49
+ ` sorted(by:) ` 方法接受一个闭包,该闭包函数需要传入与数组元素类型相同的两个值,并返回一个布尔类型值,来表明排序后第一个参数排在第二个参数前面还是后面。如果第一个参数值出现在第二个参数值* 前面* ,排序闭包函数需要返回 ` true ` ,反之返回 ` false ` 。
95
50
96
- This example is sorting an array of ` String ` values,
97
- and so the sorting closure needs to be a function of type ` (String, String) -> Bool ` .
51
+ 该例子对一个 ` String ` 类型的数组进行排序,因此排序闭包函数类型需为 ` (String, String) -> Bool ` 。
98
52
99
- One way to provide the sorting closure is to write a normal function of the correct type,
100
- and to pass it in as an argument to the ` sorted(by:) ` method:
53
+ 提供排序闭包函数的一种方式是编写一个正确类型的普通函数,并将其作为 ` sorted(by:) ` 方法的参数传入:
101
54
102
55
``` swift
103
56
func backward (_ s1 : String , _ s2 : String ) -> Bool {
@@ -120,22 +73,11 @@ var reversedNames = names.sorted(by: backward)
120
73
```
121
74
-->
122
75
123
- If the first string (` s1 ` ) is greater than the second string (` s2 ` ),
124
- the ` backward(_:_:) ` function will return ` true ` ,
125
- indicating that ` s1 ` should appear before ` s2 ` in the sorted array.
126
- For characters in strings,
127
- “greater than” means “appears later in the alphabet than”.
128
- This means that the letter ` "B" ` is “greater than” the letter ` "A" ` ,
129
- and the string ` "Tom" ` is greater than the string ` "Tim" ` .
130
- This gives a reverse alphabetical sort,
131
- with ` "Barry" ` being placed before ` "Alex" ` , and so on.
132
-
133
- However, this is a rather long-winded way to write
134
- what is essentially a single-expression function (` a > b ` ).
135
- In this example, it would be preferable to write the sorting closure inline,
136
- using closure expression syntax.
137
-
138
- ### Closure Expression Syntax
76
+ 如果第一个字符串 (` s1 ` ) 大于第二个字符串 (` s2 ` ),` backward(_:_:) ` 函数将返回 ` true ` ,表示在新的数组中 ` s1 ` 应该出现在 ` s2 ` 前。对于字符串中的字符,“大于”表示“在字母顺序较晚出现”。这意味着字母` “B” ` “大于”字母` “A” ` ,字符串` “Tom” ` 大于字符串` “Tim”。 ` 这给出了一个字母逆序排序,` 将“Barry” ` 放在` “Alex” ` 之前,依此类推。
77
+
78
+ 然而,这是一种相当繁琐的编写方式,本质上是一个单表达式函数 (` a > b ` )。对于这个例子来说,利用闭包表达式语法可以更好地构造一个内联排序闭包。
79
+
80
+ ### 闭包表达式语法
139
81
140
82
Closure expression syntax has the following general form:
141
83
0 commit comments