-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNested Types.html
More file actions
323 lines (283 loc) · 21.8 KB
/
Nested Types.html
File metadata and controls
323 lines (283 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Swift Programming Language: Nested Types</title>
<link rel="stylesheet" type="text/css" href="resource/style-1.1.15.css">
<meta charset="utf-8"> <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
<body id="conceptual_flow_with_tasks" class="jazz">
<div id="_omniture_top">
</div>
<a name="TP40014097" title="The Swift Programming Language"></a>
<section id="valence">
<div class="content-wrapper">
<p id="hierarchial_navigation">
<span id="book_title">The Swift Programming Language</span>
</p>
<img id="shortstack" src="./resource/shortstack_2x.png">
</div>
</section>
<div class="content-wrapper">
<nav class="book-parts hideInXcode" role="navigation">
<ul class="nav-parts">
<li data-id="TP40014097-CH1-XID_27" class="part-name">Welcome to Swift
<ul class="nav-chapters">
<li class="nav-chapter nav-visited-chapter">
<a href="About Swift.html#TP40014097-CH3-XID_0" data-id="TP40014097-CH3-XID_0" class="">About Swift</a>
</li>
<li class="nav-chapter">
<a href="A Swift Tour.html#TP40014097-CH2-XID_1" data-id="TP40014097-CH2-XID_1" class="">A Swift Tour</a>
</li>
</ul>
</li><li data-id="TP40014097-CH4-XID_299" class="part-name nav-part-active open-part">Language Guide
<ul class="nav-chapters" style="height: 607px;">
<li class="nav-chapter">
<a href="The Basics.html#TP40014097-CH5-XID_399" data-id="TP40014097-CH5-XID_399" class="">The Basics</a>
</li>
<li class="nav-chapter">
<a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70" class="">Basic Operators</a>
</li>
<li class="nav-chapter">
<a href="Strings and Characters.html#TP40014097-CH7-XID_368" data-id="TP40014097-CH7-XID_368" class="">Strings and Characters</a>
</li>
<li class="nav-chapter">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133" class="">Collection Types</a>
</li>
<li class="nav-chapter">
<a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153" class="">Control Flow</a>
</li>
<li class="nav-chapter">
<a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204" class="">Functions</a>
</li>
<li class="nav-chapter">
<a href="Closures.html#TP40014097-CH11-XID_117" data-id="TP40014097-CH11-XID_117" class="">Closures</a>
</li>
<li class="nav-chapter">
<a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185" class="">Enumerations</a>
</li>
<li class="nav-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94" class="">Classes and Structures</a>
</li>
<li class="nav-chapter">
<a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323" class="">Properties</a>
</li>
<li class="nav-chapter">
<a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300" class="">Methods</a>
</li>
<li class="nav-chapter">
<a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393" class="">Subscripts</a>
</li>
<li class="nav-chapter">
<a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251" class="">Inheritance</a>
</li>
<li class="nav-chapter">
<a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266" class="">Initialization</a>
</li>
<li class="nav-chapter">
<a href="Deinitialization.html#TP40014097-CH19-XID_182" data-id="TP40014097-CH19-XID_182" class="">Deinitialization</a>
</li>
<li class="nav-chapter">
<a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50" class="">Automatic Reference Counting</a>
</li>
<li class="nav-chapter">
<a href="Optional Chaining.html#TP40014097-CH21-XID_312" data-id="TP40014097-CH21-XID_312" class="">Optional Chaining</a>
</li>
<li class="nav-chapter">
<a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443" class="">Type Casting</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="Nested Types.html" data-id="TP40014097-CH23-XID_309" class="nav-chapter-active">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name">Language Reference
<ul class="nav-chapters">
<li class="nav-chapter">
<a href="About the Language Reference.html#TP40014097-CH29-XID_453" data-id="TP40014097-CH29-XID_453" class="">About the Language Reference</a>
</li>
<li class="nav-chapter">
<a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794">Lexical Structure</a>
</li>
<li class="nav-chapter">
<a href="Types.html#TP40014097-CH31-XID_988" data-id="TP40014097-CH31-XID_988">Types</a>
</li>
<li class="nav-chapter">
<a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655">Expressions</a>
</li>
<li class="nav-chapter">
<a href="Statements.html#TP40014097-CH33-XID_913" data-id="TP40014097-CH33-XID_913">Statements</a>
</li>
<li class="nav-chapter">
<a href="Declarations.html#TP40014097-CH34-XID_475" data-id="TP40014097-CH34-XID_475">Declarations</a>
</li>
<li class="nav-chapter">
<a href="Attributes.html#TP40014097-CH35-XID_460" data-id="TP40014097-CH35-XID_460">Attributes</a>
</li>
<li class="nav-chapter">
<a href="Patterns.html#TP40014097-CH36-XID_878" data-id="TP40014097-CH36-XID_878">Patterns</a>
</li>
<li class="nav-chapter">
<a href="Generic Parameters and Arguments.html#TP40014097-CH37-XID_774" data-id="TP40014097-CH37-XID_774">Generic Parameters and Arguments</a>
</li>
<li class="nav-chapter">
<a href="Summary of the Grammar.html#TP40014097-CH38-XID_1030" data-id="TP40014097-CH38-XID_1030">Summary of the Grammar</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="chapter">
<a name="TP40014097-CH23"></a><a name="TP40014097-CH23-XID_309"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Nested Types</h2>
<section id="mini_toc" class="hideInXcode" role="navigation">
<div id="mini_toc_button">
<p>On This Page</p>
</div>
<ul class="list-bullet">
<li class="item">
<p class="para">
<a href="#TP40014097-CH23-XID_310">
Nested Types in Action
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH23-XID_311">
Referring to Nested Types
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
Enumerations are often created to support a specific class or structure’s functionality. Similarly, it can be convenient to define utility classes and structures purely for use within the context of a more complex type. To accomplish this, Swift enables you to define <em>nested types</em>, whereby you nest supporting enumerations, classes, and structures within the definition of the type they support.
</p>
<p class="para">
To nest a type within another type, write its definition within the outer braces of the type it supports. Types can be nested to as many levels as are required.
</p>
</section>
<section class="section">
<a name="TP40014097-CH23-XID_310"></a>
<h3 class="section-name" tabindex="0">Nested Types in Action</h3>
<p class="para">
The example below defines a structure called <code class="code-voice">BlackjackCard</code>, which models a playing card as used in the game of Blackjack. The <code class="code-voice">BlackJack</code> structure contains two nested enumeration types called <code class="code-voice">Suit</code> and <code class="code-voice">Rank</code>.
</p><p class="para">
In Blackjack, the Ace cards have a value of either one or eleven. This feature is represented by a structure called <code class="code-voice">Values</code>, which is nested within the <code class="code-voice">Rank</code> enumeration:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">struct</span> <span class="vc">BlackjackCard</span> {</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"> <span class="c">// nested Suit enumeration</span></code></li>
<li><code class="code-voice"> <span class="kt">enum</span> <span class="vc">Suit</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">case</span> <span class="vc">Spades</span> = <span class="s">"♠"</span>, <span class="vc">Hearts</span> = <span class="s">"♡"</span>, <span class="vc">Diamonds</span> = <span class="s">"♢"</span>, <span class="vc">Clubs</span> = <span class="s">"♣"</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"> <span class="c">// nested Rank enumeration</span></code></li>
<li><code class="code-voice"> <span class="kt">enum</span> <span class="vc">Rank</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">case</span> <span class="vc">Two</span> = <span class="m">2</span>, <span class="vc">Three</span>, <span class="vc">Four</span>, <span class="vc">Five</span>, <span class="vc">Six</span>, <span class="vc">Seven</span>, <span class="vc">Eight</span>, <span class="vc">Nine</span>, <span class="vc">Ten</span></code></li>
<li><code class="code-voice"> <span class="kt">case</span> <span class="vc">Jack</span>, <span class="vc">Queen</span>, <span class="vc">King</span>, <span class="vc">Ace</span></code></li>
<li><code class="code-voice"> <span class="kt">struct</span> <span class="vc">Values</span> {</code></li>
<li><code class="code-voice"> <span class="kt">let</span> <span class="vc">first</span>: <span class="n"></span>?</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">values</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">switch</span> <span class="kt">self</span> {</code></li>
<li><code class="code-voice"> <span class="kt">case</span> .<span class="vc">Ace</span>:</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">Values</span>(<span class="vc">first</span>: <span class="m">1</span>, <span class="vc">second</span>: <span class="m">11</span>)</code></li>
<li><code class="code-voice"> <span class="kt">case</span> .<span class="vc">Jack</span>, .<span class="vc">Queen</span>, .<span class="vc">King</span>:</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">Values</span>(<span class="vc">first</span>: <span class="m">10</span>, <span class="vc">second</span>: <span class="vc">nil</span>)</code></li>
<li><code class="code-voice"> <span class="kt">default</span>:</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">Values</span>(<span class="vc">first</span>: <span class="kt">self</span>.<span class="vc">toRaw</span>(), <span class="vc">second</span>: <span class="vc">nil</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"> <span class="c">// BlackjackCard properties and methods</span></code></li>
<li><code class="code-voice"> <span class="kt">let</span> <span class="vc">rank</span>: <span class="n"></span></code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">description</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">output</span> = <span class="s">"suit is </span>\(<span class="vc">suit</span>.<span class="vc">toRaw</span>())<span class="s">,"</span></code></li>
<li><code class="code-voice"> <span class="vc">output</span> += <span class="s">" value is </span>\(<span class="vc">rank</span>.<span class="vc">values</span>.<span class="vc">first</span>)<span class="s">"</span></code></li>
<li><code class="code-voice"> <span class="kt">if</span> <span class="kt">let</span> <span class="vc">second</span> = <span class="vc">rank</span>.<span class="vc">values</span>.<span class="vc">second</span> {</code></li>
<li><code class="code-voice"> <span class="vc">output</span> += <span class="s">" or </span>\(<span class="vc">second</span>)<span class="s">"</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">output</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">Suit</code> enumeration describes the four common playing card suits, together with a raw <code class="code-voice">Character</code> value to represent their symbol.
</p><p class="para">
The <code class="code-voice">Rank</code> enumeration describes the thirteen possible playing card ranks, together with a raw <code class="code-voice">Int</code> value to represent their face value. (This raw <code class="code-voice">Int</code> value is not used for the Jack, Queen, King, and Ace cards.)
</p><p class="para">
As mentioned above, the <code class="code-voice">Rank</code> enumeration defines a further nested structure of its own, called <code class="code-voice">Values</code>. This structure encapsulates the fact that most cards have one value, but the Ace card has two values. The <code class="code-voice">Values</code> structure defines two properties to represent this:
</p><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">first</code>, of type <code class="code-voice">Int</code>
</p>
</li><li class="item"><p class="para">
<code class="code-voice">second</code>, of type <code class="code-voice">Int?</code>, or “optional <code class="code-voice">Int</code>”
</p>
</li>
</ul><p class="para">
<code class="code-voice">Rank</code> also defines a computed property, <code class="code-voice">values</code>, which returns an instance of the <code class="code-voice">Values</code> structure. This computed property considers the rank of the card and initializes a new <code class="code-voice">Values</code> instance with appropriate values based on its rank. It uses special values for <code class="code-voice">Jack</code>, <code class="code-voice">Queen</code>, <code class="code-voice">King</code>, and <code class="code-voice">Ace</code>. For the numeric cards, it uses the rank’s raw <code class="code-voice">Int</code> value.
</p><p class="para">
The <code class="code-voice">BlackjackCard</code> structure itself has two properties—<code class="code-voice">rank</code> and <code class="code-voice">suit</code>. It also defines a computed property called <code class="code-voice">description</code>, which uses the values stored in <code class="code-voice">rank</code> and <code class="code-voice">suit</code> to build a description of the name and value of the card. The <code class="code-voice">description</code> property uses optional binding to check whether there is a second value to display, and if so, inserts additional description detail for that second value.
</p><p class="para">
Because <code class="code-voice">BlackjackCard</code> is a structure with no custom initializers, it has an implicit memberwise initializer, as described in <span class="x-name"><a href="Initialization.html#TP40014097-CH18-XID_280" data-id="TP40014097-CH18-XID_280">Memberwise Initializers for Structure Types</a></span>. You can use this initializer to initialize a new constant called <code class="code-voice">theAceOfSpades</code>:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">theAceOfSpades</span> = <span class="vc">BlackjackCard</span>(<span class="vc">rank</span>: .<span class="vc">Ace</span>, <span class="vc">suit</span>: .<span class="vc">Spades</span>)</code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"theAceOfSpades: </span>\(<span class="vc">theAceOfSpades</span>.<span class="vc">description</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "theAceOfSpades: suit is ♠, value is 1 or 11"</span></code></li>
</ul>
</div>
</section><p class="para">
Even though <code class="code-voice">Rank</code> and <code class="code-voice">Suit</code> are nested within <code class="code-voice">BlackjackCard</code>, their type can be inferred from context, and so the initialization of this instance is able to refer to the enumeration members by their member names (<code class="code-voice">.Ace</code> and <code class="code-voice">.Spades</code>) alone. In the example above, the <code class="code-voice">description</code> property correctly reports that the Ace of Spades has a value of <code class="code-voice">1</code> or <code class="code-voice">11</code>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH23-XID_311"></a>
<h3 class="section-name" tabindex="0">Referring to Nested Types</h3>
<p class="para">
To use a nested type outside of its definition context, prefix its name with the name of the type it is nested within:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">heartsSymbol</span> = <span class="vc">BlackjackCard</span>.<span class="vc">Suit</span>.<span class="vc">Hearts</span>.<span class="vc">toRaw</span>()</code></li>
<li><code class="code-voice"><span class="c">// heartsSymbol is "♡"</span></code></li>
</ul>
</div>
</section><p class="para">
For the example above, this enables the names of <code class="code-voice">Suit</code>, <code class="code-voice">Rank</code>, and <code class="code-voice">Values</code> to be kept deliberately short, because their names are naturally qualified by the context in which they are defined.
</p>
</section>
<section id="next_previous" class="">
<p class="previous-link"><a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443">Type Casting</a></p>
<p class="next-link"><a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191">Extensions</a></p>
<p class="copyright">Copyright © 2014 Apple Inc. All rights reserved. <a class="link" href="http://www.apple.com/legal/terms/site.html" target="_blank" rel="external">Terms of Use</a> | <a class="link" href="http://www.apple.com/privacy/" target="_blank" rel="external">Privacy Policy</a> | Updated: 2014-06-02
</p>
</section>
</article>
</div>
<div id="big_button"></div>
<footer id="footer">
<div class="content-wrapper">
</div>
<script src="./resource/devpubs-1.1.15.js" type="text/javascript" charset="utf-8"></script> </footer> <div id="blackout"> <div id="preload"></div> </div> </body></html>