-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatterns.html
More file actions
654 lines (590 loc) · 37.1 KB
/
Patterns.html
File metadata and controls
654 lines (590 loc) · 37.1 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Swift Programming Language: Patterns</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">Language Guide
<ul class="nav-chapters">
<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">
<a href="Nested Types.html#TP40014097-CH23-XID_309" data-id="TP40014097-CH23-XID_309" class="">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191" class="">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345" class="">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234" class="">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28" class="">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name nav-part-active open-part">Language Reference
<ul class="nav-chapters" style="height: 299px;">
<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" class="">Lexical Structure</a>
</li>
<li class="nav-chapter">
<a href="Types.html#TP40014097-CH31-XID_988" data-id="TP40014097-CH31-XID_988" class="">Types</a>
</li>
<li class="nav-chapter">
<a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655" class="">Expressions</a>
</li>
<li class="nav-chapter">
<a href="Statements.html#TP40014097-CH33-XID_913" data-id="TP40014097-CH33-XID_913" class="">Statements</a>
</li>
<li class="nav-chapter">
<a href="Declarations.html#TP40014097-CH34-XID_475" data-id="TP40014097-CH34-XID_475" class="">Declarations</a>
</li>
<li class="nav-chapter">
<a href="Attributes.html#TP40014097-CH35-XID_460" data-id="TP40014097-CH35-XID_460" class="">Attributes</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="Patterns.html" data-id="TP40014097-CH36-XID_878" class="nav-chapter-active">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-CH36"></a><a name="TP40014097-CH36-XID_878"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Patterns</h2>
<section role="navigation" class="hideInXcode" id="mini_toc">
<div id="mini_toc_button">
<p>On This Page</p>
</div>
<ul class="list-bullet">
<li class="item">
<p class="para">
<a href="#TP40014097-CH36-XID_887">
Wildcard Pattern
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH36-XID_890">
Identifier Pattern
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH36-XID_893">
Value-Binding Pattern
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH36-XID_896">
Tuple Pattern
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH36-XID_901">
Enumeration Case Pattern
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH36-XID_904">
Type-Casting Patterns
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH36-XID_909">
Expression Pattern
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
A <em>pattern</em> represents the structure of a single value or a composite value. For example, the structure of a tuple <code class="code-voice">(1, 2)</code> is a comma-separated list of two elements. Because patterns represent the structure of a value rather than any one particular value, you can match them with a variety of values. For instance, the pattern <code class="code-voice">(x, y)</code> matches the tuple <code class="code-voice">(1, 2)</code> and any other two-element tuple. In addition matching a pattern with a value, you can extract part or all of a composite value and bind each part to a constant or variable name.
</p>
<p class="para">
In Swift, patterns occur in variable and constant declarations (on their left-hand side), in <code class="code-voice">for</code>-<code class="code-voice">in</code> statements, and in <code class="code-voice">switch</code> statements (in their case labels). Although any pattern can occur in the case labels of a <code class="code-voice">switch</code> statement, in the other contexts, only wildcard patterns, identifier patterns, and patterns containing those two patterns can occur.
</p>
<p class="para">
You can specify a type annotation for a wildcard pattern, an identifier pattern, and a tuple pattern to constraint the pattern to match only values of a certain type.
</p>
<div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a pattern
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/pattern"></a>
<span class="syntax-def-name">
pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/wildcard-pattern" href="#//apple_ref/swift/grammar/wildcard-pattern">wildcard-pattern</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-annotation" href="Types.html#//apple_ref/swift/grammar/type-annotation">type-annotation</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="TP40014097-CH36-XID_881"></a>
<span class="syntax-def-name">
pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/identifier-pattern" href="#//apple_ref/swift/grammar/identifier-pattern">identifier-pattern</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-annotation" href="Types.html#//apple_ref/swift/grammar/type-annotation">type-annotation</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="TP40014097-CH36-XID_882"></a>
<span class="syntax-def-name">
pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/value-binding-pattern" href="#//apple_ref/swift/grammar/value-binding-pattern">value-binding-pattern</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH36-XID_883"></a>
<span class="syntax-def-name">
pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/tuple-pattern" href="#//apple_ref/swift/grammar/tuple-pattern">tuple-pattern</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-annotation" href="Types.html#//apple_ref/swift/grammar/type-annotation">type-annotation</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="TP40014097-CH36-XID_884"></a>
<span class="syntax-def-name">
pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/enum-case-pattern" href="#//apple_ref/swift/grammar/enum-case-pattern">enum-case-pattern</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH36-XID_885"></a>
<span class="syntax-def-name">
pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-casting-pattern" href="#//apple_ref/swift/grammar/type-casting-pattern">type-casting-pattern</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH36-XID_886"></a>
<span class="syntax-def-name">
pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/expression-pattern" href="#//apple_ref/swift/grammar/expression-pattern">expression-pattern</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH36-XID_887"></a>
<h3 tabindex="0" class="section-name">Wildcard Pattern</h3>
<p class="para">
A <em>wildcard pattern</em> matches and ignores any value and consists of an underscore (<code class="code-voice">_</code>). Use a wildcard pattern when you don’t care about the values being matched against. For example, the following code iterates through the closed range <code class="code-voice">1..3</code>, ignoring the current value of the range on each iteration of the loop:
</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">for</span> <span class="kt">_</span> <span class="kt">in</span> <span class="m">1</span>...<span class="m">3</span> {</code></li>
<li><code class="code-voice"> <span class="c">// Do something three times.</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a wildcard pattern
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/wildcard-pattern"></a>
<span class="syntax-def-name">
wildcard-pattern
</span>
<span class="arrow">
→
</span><code class="literal">_</code>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH36-XID_890"></a>
<h3 tabindex="0" class="section-name">Identifier Pattern</h3>
<p class="para">
An <em>identifier pattern</em> matches any value and binds the matched value to a variable or constant name. For example, in the following constant declaration, <code class="code-voice">someValue</code> is an identifier pattern that matches the value <code class="code-voice">42</code> of type <code class="code-voice">Int</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">someValue</span> = <span class="m">42</span></code></li>
</ul>
</div>
</section><p class="para">
When the match succeeds, the value <code class="code-voice">42</code> is bound (assigned) to the constant name <code class="code-voice">someValue</code>.
</p><p class="para">
When the pattern on the left-hand side of a variable or constant declaration is an identifier pattern, the identifier pattern is implicitly a subpattern of a value-binding pattern.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an identifier pattern
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/identifier-pattern"></a>
<span class="syntax-def-name">
identifier-pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/identifier" href="Lexical Structure.html#//apple_ref/swift/grammar/identifier">identifier</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH36-XID_893"></a>
<h3 tabindex="0" class="section-name">Value-Binding Pattern</h3>
<p class="para">
A <em>value-binding pattern</em> binds matched values to variable or constant names. Value-binding patterns that bind a matched value to the name of a constant begin with the keyword <code class="code-voice">let</code>; those that bind to the name of variable begin with the keyword <code class="code-voice">var</code>.
</p><p class="para">
Identifiers patterns within a value-binding pattern bind new named variables or constants to their matching values. For example, you can decompose the elements of a tuple and bind the value of each element to a corresponding identifier pattern.
</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">point</span> = (<span class="m">3</span>, <span class="m">2</span>)</code></li>
<li><code class="code-voice"><span class="kt">switch</span> <span class="vc">point</span> {</code></li>
<li><code class="code-voice"> <span class="c">// Bind x and y to the elements of point.</span></code></li>
<li><code class="code-voice"><span class="kt">case</span> <span class="kt">let</span> (<span class="vc">x</span>, <span class="vc">y</span>):</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The point is at (</span>\(<span class="vc">x</span>)<span class="s">, </span>\(<span class="vc">y</span>)<span class="s">)."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "The point is at (3, 2)."</span></code></li>
</ul>
</div>
</section><p class="para">
In the example above, <code class="code-voice">let</code> distributes to each identifier pattern in the tuple pattern <code class="code-voice">(x, y)</code>. Because of this behavior, the <code class="code-voice">switch</code> cases <code class="code-voice">case let (x, y):</code> and <code class="code-voice">case (let x, let y):</code> match the same values.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a value-binding pattern
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/value-binding-pattern"></a>
<span class="syntax-def-name">
value-binding-pattern
</span>
<span class="arrow">
→
</span><span class="alternative">
<code class="literal">var</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern" href="#//apple_ref/swift/grammar/pattern">pattern</a></span>
</span><span class="alternative">
<code class="literal">let</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern" href="#//apple_ref/swift/grammar/pattern">pattern</a></span>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH36-XID_896"></a>
<h3 tabindex="0" class="section-name">Tuple Pattern</h3>
<p class="para">
A <em>tuple pattern</em> is a comma-separated list of zero or more patterns, enclosed in parentheses. Tuple patterns match values of corresponding tuple types.
</p><p class="para">
You can constrain a tuple pattern to match certain kinds of tuple types by using type annotations. For example, the tuple pattern <code class="code-voice">(x, y): (Int, Int)</code> in the constant declaration <code class="code-voice">let (x, y): (Int, Int) = (1, 2)</code> matches only tuple types in which both elements are of type <code class="code-voice">Int</code>. To constrain only some elements of a tuple pattern, provide type annotations directly to those individual elements. For example, the tuple pattern in <code class="code-voice">let (x: String, y)</code> matches any two-element tuple type, as long as the first element is of type <code class="code-voice">String</code>.
</p><p class="para">
When a tuple pattern is used as the pattern in a <code class="code-voice">for</code>-<code class="code-voice">in</code> statement or in a variable or constant declaration, it can contain only wildcard patterns, identifier patterns, or other tuple patterns that contain those. For example, the following code isn’t valid because the element <code class="code-voice">0</code> in the tuple pattern <code class="code-voice">(x, 0)</code> is an expression pattern:
</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">points</span> = [(<span class="m">0</span>, <span class="m">0</span>), (<span class="m">1</span>, <span class="m">0</span>), (<span class="m">1</span>, <span class="m">1</span>), (<span class="m">2</span>, <span class="m">0</span>), (<span class="m">2</span>, <span class="m">1</span>)]</code></li>
<li><code class="code-voice"><span class="c">// This code isn't valid.</span></code></li>
<li><code class="code-voice"><span class="kt">for</span> (<span class="vc">x</span>, <span class="m">0</span>) <span class="kt">in</span> <span class="vc">points</span> {</code></li>
<li><code class="code-voice"> <span class="c">/* ... */</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The parentheses around a tuple pattern that contains a single element have no effect. The pattern matches values of that single element’s type. For example, the following are equivalent:
</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">a</span> = <span class="m">2</span> <span class="c">// a: Int = 2</span></code></li>
<li><code class="code-voice"><span class="kt">let</span> (<span class="vc">a</span>) = <span class="m">2</span> <span class="c">// a: Int = 2</span></code></li>
<li><code class="code-voice"><span class="kt">let</span> (<span class="vc">a</span>): <span class="n"></span> = <span class="m">2</span> <span class="c">// a: Int = 2</span></code></li>
</ul>
</div>
</section><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a tuple pattern
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/tuple-pattern"></a>
<span class="syntax-def-name">
tuple-pattern
</span>
<span class="arrow">
→
</span><code class="literal">(</code><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/tuple-pattern-element-list" href="#//apple_ref/swift/grammar/tuple-pattern-element-list">tuple-pattern-element-list</a></span><sub class="subscript">opt</sub></span><code class="literal">)</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/tuple-pattern-element-list"></a>
<span class="syntax-def-name">
tuple-pattern-element-list
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/tuple-pattern-element" href="#//apple_ref/swift/grammar/tuple-pattern-element">tuple-pattern-element</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/tuple-pattern-element" href="#//apple_ref/swift/grammar/tuple-pattern-element">tuple-pattern-element</a></span><code class="literal">,</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/tuple-pattern-element-list" href="#//apple_ref/swift/grammar/tuple-pattern-element-list">tuple-pattern-element-list</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/tuple-pattern-element"></a>
<span class="syntax-def-name">
tuple-pattern-element
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern" href="#//apple_ref/swift/grammar/pattern">pattern</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH36-XID_901"></a>
<h3 tabindex="0" class="section-name">Enumeration Case Pattern</h3>
<p class="para">
An <em>enumeration case pattern</em> matches a case of an existing enumeration type. Enumeration case patterns appear only in <code class="code-voice">switch</code> statement case labels.
</p><p class="para">
If the enumeration case you’re trying to match has any associated values, the corresponding enumeration case pattern must specify a tuple pattern that contains one element for each associated value. For an example that uses a <code class="code-voice">switch</code> statement to match enumeration cases containing associated values, see <span class="x-name"><a data-id="TP40014097-CH12-XID_189" href="Enumerations.html#TP40014097-CH12-XID_189">Associated Values</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an enumeration case pattern
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/enum-case-pattern"></a>
<span class="syntax-def-name">
enum-case-pattern
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-identifier" href="Types.html#//apple_ref/swift/grammar/type-identifier">type-identifier</a></span><sub class="subscript">opt</sub></span><code class="literal">.</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/enum-case-name" href="Declarations.html#//apple_ref/swift/grammar/enum-case-name">enum-case-name</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/tuple-pattern" href="#//apple_ref/swift/grammar/tuple-pattern">tuple-pattern</a></span><sub class="subscript">opt</sub></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH36-XID_904"></a>
<h3 tabindex="0" class="section-name">Type-Casting Patterns</h3>
<p class="para">
There are two type-casting patterns, the <code class="code-voice">is</code> pattern and the <code class="code-voice">as</code> pattern. Both type-casting patterns appear only in <code class="code-voice">switch</code> statement case labels. The <code class="code-voice">is</code> and <code class="code-voice">as</code> patterns have the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">is</span> <em class="variable-text">type</em></pre></li><li><pre class="code-voice"><em class="variable-text">pattern</em> <span class="kt">as</span> <em class="variable-text">type</em></pre></li>
</ul>
</div><p class="para">
The <code class="code-voice">is</code> pattern matches a value if the type of that value at runtime is the same as the type specified in the right-hand side of the <code class="code-voice">is</code> pattern—or a subclass of that type. The <code class="code-voice">is</code> pattern behaves like the <code class="code-voice">is</code> operator in that they both perform a type cast but discard the returned type.
</p><p class="para">
The <code class="code-voice">as</code> pattern matches a value if the type of that value at runtime is the same as the type specified in the right-hand side of the <code class="code-voice">as</code> pattern—or a subclass of that type. If the match succeeds, the type of the matched value is cast to the <em>pattern</em> specified in the left-hand side of the <code class="code-voice">as</code> pattern.
</p><p class="para">
For an example that uses a <code class="code-voice">switch</code> statement to match values with <code class="code-voice">is</code> and <code class="code-voice">as</code> patterns, see <span class="x-name"><a data-id="TP40014097-CH22-XID_448" href="Type Casting.html#TP40014097-CH22-XID_448">Type Casting for Any and AnyObject</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a type casting pattern
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/type-casting-pattern"></a>
<span class="syntax-def-name">
type-casting-pattern
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/is-pattern" href="#//apple_ref/swift/grammar/is-pattern">is-pattern</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/as-pattern" href="#//apple_ref/swift/grammar/as-pattern">as-pattern</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/is-pattern"></a>
<span class="syntax-def-name">
is-pattern
</span>
<span class="arrow">
→
</span><code class="literal">is</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type" href="Types.html#//apple_ref/swift/grammar/type">type</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/as-pattern"></a>
<span class="syntax-def-name">
as-pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern" href="#//apple_ref/swift/grammar/pattern">pattern</a></span><code class="literal">as</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type" href="Types.html#//apple_ref/swift/grammar/type">type</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH36-XID_909"></a>
<h3 tabindex="0" class="section-name">Expression Pattern</h3>
<p class="para">
An <em>expression pattern</em> represents the value of an expression. Expression patterns appear only in <code class="code-voice">switch</code> statement case labels.
</p><p class="para">
The expression represented by the expression pattern is compared with the value of an input expression using the Swift standard library <code class="code-voice">~=</code> operator. The matches succeeds if the <code class="code-voice">~=</code> operator returns <code class="code-voice">true</code>. By default, the <code class="code-voice">~=</code> operator compares two values of the same type using the <code class="code-voice">==</code> operator. It can also match an integer value with a range of integers in an <code class="code-voice">Range</code> object, as the following example shows:
</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">point</span> = (<span class="m">1</span>, <span class="m">2</span>)</code></li>
<li><code class="code-voice"><span class="kt">switch</span> <span class="vc">point</span> {</code></li>
<li><code class="code-voice"><span class="kt">case</span> (<span class="m">0</span>, <span class="m">0</span>):</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"(0, 0) is at the origin."</span>)</code></li>
<li><code class="code-voice"><span class="kt">case</span> (-<span class="m">2</span>...<span class="m">2</span>, -<span class="m">2</span>...<span class="m">2</span>):</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"(</span>\(<span class="vc">point</span>.<span class="m">0</span>)<span class="s">, </span>\(<span class="vc">point</span>.<span class="m">1</span>)<span class="s">) is near the origin."</span>)</code></li>
<li><code class="code-voice"><span class="kt">default</span>:</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The point is at (</span>\(<span class="vc">point</span>.<span class="m">0</span>)<span class="s">, </span>\(<span class="vc">point</span>.<span class="m">1</span>)<span class="s">)."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "(1, 2) is near the origin."</span></code></li>
</ul>
</div>
</section><p class="para">
You can overload the <code class="code-voice">~=</code> operator to provide custom expression matching behavior. For example, you can rewrite the above example to compare the <code class="code-voice">point</code> expression with a string representations of points.
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="c">// Overload the ~= operator to match a string with an integer</span></code></li>
<li><code class="code-voice"><span class="kt">func</span> ~=(<span class="vc">pattern</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">pattern</span> == <span class="s">"</span>\(<span class="vc">value</span>)<span class="s">"</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="kt">switch</span> <span class="vc">point</span> {</code></li>
<li><code class="code-voice"><span class="kt">case</span> (<span class="s">"0"</span>, <span class="s">"0"</span>):</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"(0, 0) is at the origin."</span>)</code></li>
<li><code class="code-voice"><span class="kt">case</span> (<span class="s">"-2...2"</span>, <span class="s">"-2...2"</span>):</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"(</span>\(<span class="vc">point</span>.<span class="m">0</span>)<span class="s">, </span>\(<span class="vc">point</span>.<span class="m">1</span>)<span class="s">) is near the origin."</span>)</code></li>
<li><code class="code-voice"><span class="kt">default</span>:</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The point is at (</span>\(<span class="vc">point</span>.<span class="m">0</span>)<span class="s">, </span>\(<span class="vc">point</span>.<span class="m">1</span>)<span class="s">)."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "(1, 2) is near the origin."</span></code></li>
</ul>
</div>
</section><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an expression pattern
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/expression-pattern"></a>
<span class="syntax-def-name">
expression-pattern
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/expression" href="Expressions.html#//apple_ref/swift/grammar/expression">expression</a></span>
</p>
</div>
</div>
</section>
<section class="" id="next_previous">
<p class="previous-link"><a data-id="TP40014097-CH35-XID_460" href="Attributes.html#TP40014097-CH35-XID_460">Attributes</a></p>
<p class="next-link"><a data-id="TP40014097-CH37-XID_774" href="Generic Parameters and Arguments.html#TP40014097-CH37-XID_774">Generic Parameters and Arguments</a></p>
<p class="copyright">Copyright © 2014 Apple Inc. All rights reserved. <a rel="external" target="_blank" href="http://www.apple.com/legal/terms/site.html" class="link">Terms of Use</a> | <a rel="external" target="_blank" href="http://www.apple.com/privacy/" class="link">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>