1
- <!--
2
- 要翻译的文件:https://github.com/SwiftGGTeam/the-swift-programming-language-in-chinese/blob/swift-6-beta-translation/swift-6-beta.docc/LanguageGuide/NestedTypes.md
3
- Swift 文档源文件地址:https://docs.swift.org/swift-book/documentation/the-swift-programming-language/nestedtypes
4
- 翻译估计用时:⭐️⭐️⭐️
5
- -->
6
-
7
- # Nested Types
1
+ # 嵌套类型
8
2
9
- Define types inside the scope of another type.
3
+ 在另一个类型的作用域内定义类型。
10
4
11
- Enumerations are often created to support a specific class or structure's functionality.
12
- Similarly, it can be convenient to define utility structures
13
- purely for use within the context of a more complex type,
14
- and protocols that are normally used in conjunction with a specific type.
15
- To accomplish this, Swift enables you to define * nested types* ,
16
- whereby you nest supporting types like enumerations, structures, and protocols
17
- within the definition of the type they support.
5
+ 枚举常被创建以支持特定类或结构的功能。同样的,为了在更复杂类型的上下文中使用而定义纯工具性结构,以及通常与特定类型结合使用的协议,也显得十分便利。为了实现这一点,Swift 允许你定义 * 嵌套类型* ,即在支持的类型定义中嵌套枚举、结构和协议等辅助类型。
18
6
19
- To nest a type within another type,
20
- write its definition within the outer braces of the type it supports.
21
- Types can be nested to as many levels as are required.
7
+ 要在一个类型中嵌套另一个类型,只需将其定义写在外部类型的大括号内。类型可以根据需要嵌套到任意层次。
22
8
23
- ## Nested Types in Action
9
+ ## 使用嵌套类型
24
10
25
- The example below defines a structure called ` BlackjackCard ` ,
26
- which models a playing card as used in the game of Blackjack.
27
- The ` BlackjackCard ` structure contains two nested enumeration types
28
- called ` Suit ` and ` Rank ` .
11
+ 下面的示例定义了一个名为` BlackjackCard ` 的结构,用于模拟 Blackjack 游戏中的扑克牌。` BlackjackCard ` 结构包含两个嵌套的枚举类型,分别是 ` Suit ` 和` Rank ` 。
29
12
30
- In Blackjack, the Ace cards have a value of either one or eleven.
31
- This feature is represented by a structure called ` Values ` ,
32
- which is nested within the ` Rank ` enumeration:
13
+ 在 Blackjack 中,Ace 牌的值可以是 1 或 11。这个特性通过嵌套在 ` Rank ` 枚举中的一个名为 ` Values ` 的结构表示:
33
14
34
15
``` swift
35
16
struct BlackjackCard {
36
17
37
- // nested Suit enumeration
18
+ // 嵌套的 Suit 枚举
38
19
enum Suit : Character {
39
20
case spades = " ♠" , hearts = " ♡" , diamonds = " ♢" , clubs = " ♣"
40
21
}
41
22
42
- // nested Rank enumeration
23
+ // 嵌套的 Rank 枚举
43
24
enum Rank : Int {
44
25
case two = 2 , three , four , five , six , seven , eight , nine , ten
45
26
case jack , queen , king , ace
@@ -58,7 +39,7 @@ struct BlackjackCard {
58
39
}
59
40
}
60
41
61
- // BlackjackCard properties and methods
42
+ // BlackjackCard 属性和方法
62
43
let rank: Rank, suit: Suit
63
44
var description: String {
64
45
var output = " suit is \( suit.rawValue ) ,"
@@ -115,46 +96,28 @@ struct BlackjackCard {
115
96
```
116
97
-->
117
98
118
- The ` Suit ` enumeration describes the four common playing card suits,
119
- together with a raw ` Character ` value to represent their symbol.
120
-
121
- The ` Rank ` enumeration describes the thirteen possible playing card ranks,
122
- together with a raw ` Int ` value to represent their face value.
123
- (This raw ` Int ` value isn't used for the Jack, Queen, King, and Ace cards.)
124
-
125
- As mentioned above, the ` Rank ` enumeration defines
126
- a further nested structure of its own, called ` Values ` .
127
- This structure encapsulates the fact that most cards have one value,
128
- but the Ace card has two values.
129
- The ` Values ` structure defines two properties to represent this:
130
-
131
- - ` first ` , of type ` Int `
132
- - ` second ` , of type ` Int? ` , or “optional ` Int ` ”
133
-
134
- ` Rank ` also defines a computed property, ` values ` ,
135
- which returns an instance of the ` Values ` structure.
136
- This computed property considers the rank of the card
137
- and initializes a new ` Values ` instance with appropriate values based on its rank.
138
- It uses special values for ` jack ` , ` queen ` , ` king ` , and ` ace ` .
139
- For the numeric cards, it uses the rank's raw ` Int ` value.
140
-
141
- The ` BlackjackCard ` structure itself has two properties --- ` rank ` and ` suit ` .
142
- It also defines a computed property called ` description ` ,
143
- which uses the values stored in ` rank ` and ` suit ` to build
144
- a description of the name and value of the card.
145
- The ` description ` property uses optional binding to check whether there's
146
- a second value to display, and if so,
147
- inserts additional description detail for that second value.
148
-
149
- Because ` BlackjackCard ` is a structure with no custom initializers,
150
- it has an implicit memberwise initializer,
151
- as described in < doc:Initialization#Memberwise-Initializers-for-Structure-Types > .
152
- You can use this initializer to initialize a new constant called ` theAceOfSpades ` :
99
+ ` Suit ` 枚举描述了四种常见的扑克牌花色,以及用于表示其符号的原始 ` Character ` 值。
100
+
101
+ ` Rank ` 枚举描述了十三种可能的扑克牌等级,以及用于表示其数值的原始 ` Int ` 值
102
+ (这个原始的 ` Int ` 值不适用于杰克 (Jack)、皇后 (Queen)、国王 (King) 和王牌 (Ace) 牌。)
103
+
104
+ 正如上面提到的,` Rank ` 枚举定义了一个它自己的进一步嵌套的结构,叫做 ` Values ` 。
105
+ 这个结构封装了大多数牌只有一个值,而王牌 (Ace) 有两个值的事实。
106
+ ` Values ` 结构定义了两个属性来表示这一点:
107
+
108
+ - ` first ` ,类型为 ` Int `
109
+ - ` second ` ,类型为 ` Int? ` ,或“可选的 ` Int ` ”
110
+
111
+ ` Rank ` 还定义了一个计算属性` values ` ,它返回一个 ` Values ` 结构的实例。 该计算属性会根据牌的等级 (rank) 生成一个新的` Values ` 实例,并为不同的等级分配合适的值。 对于` jack ` 、` queen ` 、` king ` 和` ace ` ,使用特殊的值。 对于数字牌,则使用等级的原始 ` Int ` 值。
112
+
113
+ ` BlackjackCard ` 结构本身有两个属性 —— ` rank ` 和 ` suit ` 。它还定义了一个名为 ` description ` 的计算属性,该属性使用 ` rank ` 和 ` suit ` 中存储的值来构建牌的名称和数值的描述。` description ` 属性使用可选绑定来检查是否存在第二个值,如果有,则为第二个值插入额外的描述信息。
114
+
115
+ 由于 ` BlackjackCard ` 是一个没有自定义初始化器的结构体,它具有一个隐式的成员逐一初始化器,正如在 [[ doc: Initialization #Memberwise-Initializers-for-Structure-Types] ( doc:Initialization#Memberwise-Initializers-for-Structure-Types )] 中所描述的那样。你可以使用这个初始化器来初始化一个名为 ` theAceOfSpades ` 的新常量:
153
116
154
117
``` swift
155
118
let theAceOfSpades = BlackjackCard (rank : .ace , suit : .spades )
156
119
print (" theAceOfSpades: \( theAceOfSpades.description ) " )
157
- // Prints "theAceOfSpades: suit is ♠, value is 1 or 11"
120
+ // 输出 "theAceOfSpades: suit is ♠, value 是 1 或者 11"
158
121
```
159
122
160
123
<!--
@@ -167,21 +130,16 @@ print("theAceOfSpades: \(theAceOfSpades.description)")
167
130
```
168
131
-->
169
132
170
- Even though ` Rank ` and ` Suit ` are nested within ` BlackjackCard ` ,
171
- their type can be inferred from context,
172
- and so the initialization of this instance is able to refer to the enumeration cases
173
- by their case names (` .ace ` and ` .spades ` ) alone.
174
- In the example above, the ` description ` property correctly reports that
175
- the Ace of Spades has a value of ` 1 ` or ` 11 ` .
133
+ 尽管 ` Rank ` 和 ` Suit ` 嵌套在 ` BlackjackCard ` 中,它们的类型可以从上下文中推断出来,因此在初始化这个实例时,可以仅通过它们的名称(如 ` .ace ` 和 ` .spades ` )来引用枚举的情况。
134
+ 在上面的示例中,` description ` 属性正确地记录了黑桃 A 的值为 ` 1 ` 或 ` 11 ` 。
176
135
177
- ## Referring to Nested Types
136
+ ## 访问嵌套类型
178
137
179
- To use a nested type outside of its definition context,
180
- prefix its name with the name of the type it's nested within:
138
+ 要在定义上下文之外使用嵌套类型,需要在其名称前加上其外部类型的名称前缀:
181
139
182
140
``` swift
183
141
let heartsSymbol = BlackjackCard.Suit .hearts .rawValue
184
- // heartsSymbol is "♡"
142
+ // heartsSymbol 是 "♡"
185
143
```
186
144
187
145
<!--
@@ -194,15 +152,12 @@ let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
194
152
```
195
153
-->
196
154
197
- For the example above,
198
- this enables the names of ` Suit ` , ` Rank ` , and ` Values ` to be kept deliberately short,
199
- because their names are naturally qualified by the context in which they're defined.
155
+ 在上面的例子中,这使得 ` Suit ` , ` Rank ` , 和 ` Values ` 的名称可以故意保持简短,因为它们的名称由定义它们的上下文自然限定。
200
156
201
- > Beta Software:
202
- >
203
- > This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.
204
- >
205
- > Learn more about using [ Apple's beta software] ( https://developer.apple.com/support/beta-software/ ) .
157
+ > Beta 软件:
158
+ > 此文档包含关于 API 或开发中技术的初步信息。此信息可能会发生更改,依照此文档实现的软件应使用最终操作系统软件进行测试。
159
+ >
160
+ > 了解更多关于使用 [ Apple's 测试软件] ( https://developer.apple.com/support/beta-software/ ) .的信息。
206
161
207
162
<!--
208
163
This source file is part of the Swift.org open source project
@@ -213,3 +168,5 @@ Licensed under Apache License v2.0 with Runtime Library Exception
213
168
See https://swift.org/LICENSE.txt for license information
214
169
See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
215
170
-->
171
+
172
+
0 commit comments