Skip to content

Commit 9ad7183

Browse files
author
barberchen
committed
LanguageGuide / nested-types.md ⭐️⭐️⭐️ #1327
1 parent 05d62a3 commit 9ad7183

File tree

1 file changed

+40
-77
lines changed

1 file changed

+40
-77
lines changed
Lines changed: 40 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
1-
# Nested Types
1+
# 嵌套类型
22

3-
Define types inside the scope of another type.
3+
在另一个类型的作用域内定义类型。
44

5-
Enumerations are often created to support a specific class or structure's functionality.
6-
Similarly, it can be convenient to define utility structures
7-
purely for use within the context of a more complex type,
8-
and protocols that are normally used in conjunction with a specific type.
9-
To accomplish this, Swift enables you to define *nested types*,
10-
whereby you nest supporting types like enumerations, structures, and protocols
11-
within the definition of the type they support.
5+
枚举通常用于支持特定类或结构的功能。同样,仅在更复杂类型的上下文中使用的实用结构的定义也非常方便,协议通常与特定类型一起使用。为了实现这一点,Swift 允许你定义 *嵌套类型*,即在支持的类型定义中嵌套枚举、结构和协议等辅助类型。
126

13-
To nest a type within another type,
14-
write its definition within the outer braces of the type it supports.
15-
Types can be nested to as many levels as are required.
7+
要在一个类型中嵌套另一个类型,只需将其定义写在外部类型的大括号内。类型可以根据需要嵌套到任意层次。
168

17-
## Nested Types in Action
9+
## 使用嵌套类型
1810

19-
The example below defines a structure called `BlackjackCard`,
20-
which models a playing card as used in the game of Blackjack.
21-
The `BlackjackCard` structure contains two nested enumeration types
22-
called `Suit` and `Rank`.
11+
下面的示例定义了一个名为`BlackjackCard` 的结构,用于模拟 Blackjack 游戏中的扑克牌。`BlackjackCard`结构包含两个嵌套的枚举类型,分别是 `Suit``Rank`
2312

24-
In Blackjack, the Ace cards have a value of either one or eleven.
25-
This feature is represented by a structure called `Values`,
26-
which is nested within the `Rank` enumeration:
13+
在 Blackjack 中,Ace 牌的值可以是 1 或 11。这个特性通过嵌套在 `Rank`枚举中的一个名为 `Values` 的结构表示:
2714

2815
```swift
2916
struct BlackjackCard {
3017

31-
// nested Suit enumeration
18+
// 嵌套的 Suit 枚举
3219
enum Suit: Character {
3320
case spades = "", hearts = "", diamonds = "", clubs = ""
3421
}
3522

36-
// nested Rank enumeration
23+
// 嵌套的 Rank 枚举
3724
enum Rank: Int {
3825
case two = 2, three, four, five, six, seven, eight, nine, ten
3926
case jack, queen, king, ace
@@ -52,7 +39,7 @@ struct BlackjackCard {
5239
}
5340
}
5441

55-
// BlackjackCard properties and methods
42+
// BlackjackCard 属性和方法
5643
let rank: Rank, suit: Suit
5744
var description: String {
5845
var output = "suit is \(suit.rawValue),"
@@ -109,46 +96,28 @@ struct BlackjackCard {
10996
```
11097
-->
11198

112-
The `Suit` enumeration describes the four common playing card suits,
113-
together with a raw `Character` value to represent their symbol.
114-
115-
The `Rank` enumeration describes the thirteen possible playing card ranks,
116-
together with a raw `Int` value to represent their face value.
117-
(This raw `Int` value isn't used for the Jack, Queen, King, and Ace cards.)
118-
119-
As mentioned above, the `Rank` enumeration defines
120-
a further nested structure of its own, called `Values`.
121-
This structure encapsulates the fact that most cards have one value,
122-
but the Ace card has two values.
123-
The `Values` structure defines two properties to represent this:
124-
125-
- `first`, of type `Int`
126-
- `second`, of type `Int?`, or “optional `Int`
127-
128-
`Rank` also defines a computed property, `values`,
129-
which returns an instance of the `Values` structure.
130-
This computed property considers the rank of the card
131-
and initializes a new `Values` instance with appropriate values based on its rank.
132-
It uses special values for `jack`, `queen`, `king`, and `ace`.
133-
For the numeric cards, it uses the rank's raw `Int` value.
134-
135-
The `BlackjackCard` structure itself has two properties --- `rank` and `suit`.
136-
It also defines a computed property called `description`,
137-
which uses the values stored in `rank` and `suit` to build
138-
a description of the name and value of the card.
139-
The `description` property uses optional binding to check whether there's
140-
a second value to display, and if so,
141-
inserts additional description detail for that second value.
142-
143-
Because `BlackjackCard` is a structure with no custom initializers,
144-
it has an implicit memberwise initializer,
145-
as described in <doc:Initialization#Memberwise-Initializers-for-Structure-Types>.
146-
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` 的新常量:
147116

148117
```swift
149118
let theAceOfSpades = BlackjackCard(rank: .ace, suit: .spades)
150119
print("theAceOfSpades: \(theAceOfSpades.description)")
151-
// Prints "theAceOfSpades: suit is ♠, value is 1 or 11"
120+
// 输出 "theAceOfSpades: suit is ♠, value 1 或者 11"
152121
```
153122

154123
<!--
@@ -161,21 +130,16 @@ print("theAceOfSpades: \(theAceOfSpades.description)")
161130
```
162131
-->
163132

164-
Even though `Rank` and `Suit` are nested within `BlackjackCard`,
165-
their type can be inferred from context,
166-
and so the initialization of this instance is able to refer to the enumeration cases
167-
by their case names (`.ace` and `.spades`) alone.
168-
In the example above, the `description` property correctly reports that
169-
the Ace of Spades has a value of `1` or `11`.
133+
尽管 `Rank``Suit` 嵌套在 `BlackjackCard` 中,它们的类型可以从上下文中推断出来,因此在初始化这个实例时,可以仅通过它们的名称(如 `.ace``.spades`)来引用枚举的情况。
134+
在上面的示例中,`description` 属性正确地报告了黑桃 A 的值为 `1``11`
170135

171-
## Referring to Nested Types
136+
## 访问嵌套类型
172137

173-
To use a nested type outside of its definition context,
174-
prefix its name with the name of the type it's nested within:
138+
要在定义上下文之外使用嵌套类型,需要在其名称前加上其外部类型的名称前缀:
175139

176140
```swift
177141
let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
178-
// heartsSymbol is "♡"
142+
// heartsSymbol "♡"
179143
```
180144

181145
<!--
@@ -188,15 +152,12 @@ let heartsSymbol = BlackjackCard.Suit.hearts.rawValue
188152
```
189153
-->
190154

191-
For the example above,
192-
this enables the names of `Suit`, `Rank`, and `Values` to be kept deliberately short,
193-
because their names are naturally qualified by the context in which they're defined.
155+
在上面的例子中,这使得 `Suit`, `Rank`, 和 `Values`的名称可以故意保持简短,因为它们的名称由定义它们的上下文自然限定。
194156

195-
> Beta Software:
196-
>
197-
> 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.
198-
>
199-
> 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/).的信息。
200161
201162
<!--
202163
This source file is part of the Swift.org open source project
@@ -207,3 +168,5 @@ Licensed under Apache License v2.0 with Runtime Library Exception
207168
See https://swift.org/LICENSE.txt for license information
208169
See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
209170
-->
171+
172+

0 commit comments

Comments
 (0)