-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerics.html
More file actions
1012 lines (860 loc) · 76.8 KB
/
Generics.html
File metadata and controls
1012 lines (860 loc) · 76.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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Swift Programming Language: Generics</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">
<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 nav-current-chapter">
<a href="Generics.html" data-id="TP40014097-CH26-XID_234" class="nav-chapter-active">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-CH26"></a><a name="TP40014097-CH26-XID_234"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Generics</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-CH26-XID_235">
The Problem That Generics Solve
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH26-XID_237">
Generic Functions
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH26-XID_239">
Type Parameters
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH26-XID_240">
Naming Type Parameters
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH26-XID_242">
Generic Types
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH26-XID_244">
Type Constraints
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH26-XID_247">
Associated Types
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH26-XID_250">
Where Clauses
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
<em>Generic code</em> enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.
</p>
<p class="para">
Generics are one of the most powerful features of Swift, and much of the Swift standard library is built with generic code. In fact, you’ve been using generics throughout this Language Guide, even if you didn’t realize it. For example, Swift’s <code class="code-voice">Array</code> and <code class="code-voice">Dictionary</code> types are both generic collections. You can create an array that holds <code class="code-voice">Int</code> values, or an array that holds <code class="code-voice">String</code> values, or indeed an array for any other type that can be created in Swift. Similarly, you can create a dictionary to store values of any specified type, and there are no limitations on what that type can be.
</p>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_235"></a>
<h3 class="section-name" tabindex="0">The Problem That Generics Solve</h3>
<p class="para">
Here’s a standard, non-generic function called <code class="code-voice">swapTwoInts</code>, which swaps two <code class="code-voice">Int</code> values:
</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">func</span> <span class="vc">swapTwoInts</span>(<span class="kt">inout</span> <span class="vc">a</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="kt">let</span> <span class="vc">temporaryA</span> = <span class="vc">a</span></code></li>
<li><code class="code-voice"> <span class="vc">a</span> = <span class="vc">b</span></code></li>
<li><code class="code-voice"> <span class="vc">b</span> = <span class="vc">temporaryA</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
This function makes use of in-out parameters to swap the values of <code class="code-voice">a</code> and <code class="code-voice">b</code>, as described in <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_226" data-id="TP40014097-CH10-XID_226">In-Out Parameters</a></span>.
</p><p class="para">
The <code class="code-voice">swapTwoInts</code> function swaps the original value of <code class="code-voice">b</code> into <code class="code-voice">a</code>, and the original value of <code class="code-voice">a</code> into <code class="code-voice">b</code>. You can call this function to swap the values in two <code class="code-voice">Int</code> variables:
</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">var</span> <span class="vc">someInt</span> = <span class="m">3</span></code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">anotherInt</span> = <span class="m">107</span></code></li>
<li><code class="code-voice"><span class="vc">swapTwoInts</span>(&<span class="vc">someInt</span>, &<span class="vc">anotherInt</span>)</code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"someInt is now </span>\(<span class="vc">someInt</span>)<span class="s">, and anotherInt is now </span>\(<span class="vc">anotherInt</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "someInt is now 107, and anotherInt is now 3"</span></code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">swapTwoInts</code> function is useful, but it can only be used with <code class="code-voice">Int</code> values. If you want to swap two <code class="code-voice">String</code> values, or two <code class="code-voice">Double</code> values, you have to write more functions, such as the <code class="code-voice">swapTwoStrings</code> and <code class="code-voice">swapTwoDoubles</code> functions shown below:
</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">func</span> <span class="vc">swapTwoStrings</span>(<span class="kt">inout</span> <span class="vc">a</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="kt">let</span> <span class="vc">temporaryA</span> = <span class="vc">a</span></code></li>
<li><code class="code-voice"> <span class="vc">a</span> = <span class="vc">b</span></code></li>
<li><code class="code-voice"> <span class="vc">b</span> = <span class="vc">temporaryA</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="kt">func</span> <span class="vc">swapTwoDoubles</span>(<span class="kt">inout</span> <span class="vc">a</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="kt">let</span> <span class="vc">temporaryA</span> = <span class="vc">a</span></code></li>
<li><code class="code-voice"> <span class="vc">a</span> = <span class="vc">b</span></code></li>
<li><code class="code-voice"> <span class="vc">b</span> = <span class="vc">temporaryA</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
You may have noticed that the bodies of the <code class="code-voice">swapTwoInts</code>, <code class="code-voice">swapTwoStrings</code>, and <code class="code-voice">swapTwoDoubles</code> functions are identical. The only difference is the type of the values that they accept (<code class="code-voice">Int</code>, <code class="code-voice">String</code>, and <code class="code-voice">Double</code>).
</p><p class="para">
It would be much more useful, and considerably more flexible, to write a single function that could swap two values of <em>any</em> type. This is the kind of problem that generic code can solve. (A generic version of these functions is defined below.)
</p><div class="note">
<a name="TP40014097-CH26-XID_236"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">In all three functions, it is important that the types of <code class="code-voice">a</code> and <code class="code-voice">b</code> are defined to be the same as each other. If <code class="code-voice">a</code> and <code class="code-voice">b</code> were not of the same type, it would not be possible to swap their values. Swift is a type-safe language, and does not allow (for example) a variable of type <code class="code-voice">String</code> and a variable of type <code class="code-voice">Double</code> to swap values with each other. Attempting to do so would be reported as a compile-time error.
</p>
</aside>
</div>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_237"></a>
<h3 class="section-name" tabindex="0">Generic Functions</h3>
<p class="para">
<em>Generic functions</em> can work with any type. Here’s a generic version of the <code class="code-voice">swapTwoInts</code> function from above, called <code class="code-voice">swapTwoValues</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">func</span> <span class="vc">swapTwoValues</span><<span class="vc">T</span>>(<span class="kt">inout</span> <span class="vc">a</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="kt">let</span> <span class="vc">temporaryA</span> = <span class="vc">a</span></code></li>
<li><code class="code-voice"> <span class="vc">a</span> = <span class="vc">b</span></code></li>
<li><code class="code-voice"> <span class="vc">b</span> = <span class="vc">temporaryA</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The body of the <code class="code-voice">swapTwoValues</code> function is identical to the body of the <code class="code-voice">swapTwoInts</code> function. However, the first line of <code class="code-voice">swapTwoValues</code> is slightly different from <code class="code-voice">swapTwoInts</code>. Here’s how the first lines compare:
</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">func</span> <span class="vc">swapTwoInts</span>(<span class="kt">inout</span> <span class="vc">a</span>: <span class="n"></span>)</code></li>
<li><code class="code-voice"><span class="kt">func</span> <span class="vc">swapTwoValues</span><<span class="vc">T</span>>(<span class="kt">inout</span> <span class="vc">a</span>: <span class="n"></span>)</code></li>
</ul>
</div>
</section><p class="para">
The generic version of the function uses a <em>placeholder</em> type name (called <code class="code-voice">T</code>, in this case) instead of an <em>actual</em> type name (such as <code class="code-voice">Int</code>, <code class="code-voice">String</code>, or <code class="code-voice">Double</code>). The placeholder type name doesn’t say anything about what <code class="code-voice">T</code> must be, but it <em>does</em> say that both <code class="code-voice">a</code> and <code class="code-voice">b</code> must be of the same type <code class="code-voice">T</code>, whatever <code class="code-voice">T</code> represents. The actual type to use in place of <code class="code-voice">T</code> will be determined each time the <code class="code-voice">swapTwoValues</code> function is called.
</p><p class="para">
The other difference is that the generic function’s name (<code class="code-voice">swapTwoValues</code>) is followed by the placeholder type name (<code class="code-voice">T</code>) inside angle brackets (<code class="code-voice"><T></code>). The brackets tell Swift that <code class="code-voice">T</code> is a placeholder type name within the <code class="code-voice">swapTwoValues</code> function definition. Because <code class="code-voice">T</code> is a placeholder, Swift does not look for an actual type called <code class="code-voice">T</code>.
</p><p class="para">
The <code class="code-voice">swapTwoValues</code> function can now be called in the same way as <code class="code-voice">swapTwoInts</code>, except that it can be passed two values of <em>any</em> type, as long as both of those values are of the same type as each other. Each time <code class="code-voice">swapTwoValues</code> is called, the type to use for <code class="code-voice">T</code> is inferred from the types of values passed to the function.
</p><p class="para">
In the two examples below, <code class="code-voice">T</code> is inferred to be <code class="code-voice">Int</code> and <code class="code-voice">String</code> respectively:
</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">var</span> <span class="vc">someInt</span> = <span class="m">3</span></code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">anotherInt</span> = <span class="m">107</span></code></li>
<li><code class="code-voice"><span class="vc">swapTwoValues</span>(&<span class="vc">someInt</span>, &<span class="vc">anotherInt</span>)</code></li>
<li><code class="code-voice"><span class="c">// someInt is now 107, and anotherInt is now 3</span></code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">someString</span> = <span class="s">"hello"</span></code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">anotherString</span> = <span class="s">"world"</span></code></li>
<li><code class="code-voice"><span class="vc">swapTwoValues</span>(&<span class="vc">someString</span>, &<span class="vc">anotherString</span>)</code></li>
<li><code class="code-voice"><span class="c">// someString is now "world", and anotherString is now "hello"</span></code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH26-XID_238"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">The <code class="code-voice">swapTwoValues</code> function defined above is inspired by a generic function called <code class="code-voice">swap</code>, which is part of the Swift standard library, and is automatically made available for you to use in your apps. If you need the behavior of the <code class="code-voice">swapTwoValues</code> function in your own code, you can use Swift’s existing <code class="code-voice">swap</code> function rather than providing your own implementation.
</p>
</aside>
</div>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_239"></a>
<h3 class="section-name" tabindex="0">Type Parameters</h3>
<p class="para">
In the <code class="code-voice">swapTwoValues</code> example above, the placeholder type <code class="code-voice">T</code> is an example of a <em>type parameter</em>. Type parameters specify and name a placeholder type, and are written immediately after the function’s name, between a pair of matching angle brackets (such as <code class="code-voice"><T></code>).
</p><p class="para">
Once specified, a type parameter can be used to define the type of a function’s parameters (such as the <code class="code-voice">a</code> and <code class="code-voice">b</code> parameters of the <code class="code-voice">swapTwoValues</code> function); or as the function’s return type; or as a type annotation within the body of the function. In each case, the placeholder type represented by the type parameter is replaced with an <em>actual</em> type whenever the function is called. (In the <code class="code-voice">swapTwoValues</code> example above, <code class="code-voice">T</code> was replaced with <code class="code-voice">Int</code> the first time the function was called, and was replaced with <code class="code-voice">String</code> the second time it was called.)
</p><p class="para">
You can provide more than one type parameter by writing multiple type parameter names within the angle brackets, separated by commas.
</p>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_240"></a>
<h3 class="section-name" tabindex="0">Naming Type Parameters</h3>
<p class="para">
In simple cases where a generic function or generic type needs to refer to a single placeholder type (such as the <code class="code-voice">swapTwoValues</code> generic function above, or a generic collection that stores a single type, such as <code class="code-voice">Array</code>), it is traditional to use the single-character name <code class="code-voice">T</code> for the type parameter. However, you are can use any valid identifier as the type parameter name.
</p><p class="para">
If you are defining more complex generic functions, or generic types with multiple parameters, it can be useful to provide more descriptive type parameter names. For example, Swift’s <code class="code-voice">Dictionary</code> type has two type parameters—one for its keys and one for its values. If you were writing <code class="code-voice">Dictionary</code> yourself, you might name these two type parameters <code class="code-voice">KeyType</code> and <code class="code-voice">ValueType</code> to remind you of their purpose as you use them within your generic code.
</p><div class="note">
<a name="TP40014097-CH26-XID_241"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Always give type parameters <code class="code-voice">UpperCamelCase</code> names (such as <code class="code-voice">T</code> and <code class="code-voice">KeyType</code>) to indicate that they are a placeholder for a <em>type</em>, not a value.
</p>
</aside>
</div>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_242"></a>
<h3 class="section-name" tabindex="0">Generic Types</h3>
<p class="para">
In addition to generic functions, Swift enables you to define your own <em>generic types</em>. These are custom classes, structures, and enumerations that can work with <em>any</em> type, in a similar way to <code class="code-voice">Array</code> and <code class="code-voice">Dictionary</code>.
</p><p class="para">
This section shows you how to write a generic collection type called <code class="code-voice">Stack</code>. A stack is an ordered set of values, similar to an array, but with a more restricted set of operations than Swift’s <code class="code-voice">Array</code> type. An array allows new items to be inserted and removed at any location in the array. A stack, however, allows new items to be appended only to the end of the collection (known as <em>pushing</em> a new value on to the stack). Similarly, a stack allows items to be removed only from the end of the collection (known as <em>popping</em> a value off the stack).
</p><div class="note">
<a name="TP40014097-CH26-XID_243"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">The concept of a stack is used by the <code class="code-voice">UINavigationController</code> class to model the view controllers in its navigation hierarchy. You call the <code class="code-voice">UINavigationController</code> class <code class="code-voice">pushViewController:animated:</code> method to add (or push) a view controller on to the navigation stack, and its <code class="code-voice">popViewControllerAnimated:</code> method to remove (or pop) a view controller from the navigation stack. A stack is a useful collection model whenever you need a strict “last in, first out” approach to managing a collection.
</p>
</aside>
</div><p class="para">
The illustration below shows the push / pop behavior for a stack:
</p><figure class="figure">
<span class="caption"></span>
<img src="./resource/stackPushPop_2x.png" alt="image: ../Art/stackPushPop_2x.png" width="664" height="273">
</figure><ol class="list-number">
<li class="item"><p class="para">
There are currently three values on the stack.
</p>
</li><li class="item"><p class="para">
A fourth value is “pushed” on to the top of the stack.
</p>
</li><li class="item"><p class="para">
The stack now holds four values, with the most recent one at the top.
</p>
</li><li class="item"><p class="para">
The top item in the stack is removed, or “popped”.
</p>
</li><li class="item"><p class="para">
After popping a value, the stack once again holds three values.
</p>
</li>
</ol><p class="para">
Here’s how to write a non-generic version of a stack, in this case for a stack of <code class="code-voice">Int</code> values:
</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">IntStack</span> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">items</span> = <span class="vc">Int</span>[]()</code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">push</span>(<span class="vc">item</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="vc">items</span>.<span class="vc">append</span>(<span class="vc">item</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">pop</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">items</span>.<span class="vc">removeLast</span>()</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
This structure uses an <code class="code-voice">Array</code> property called <code class="code-voice">items</code> to store the values in the stack. <code class="code-voice">Stack</code> provides two methods, <code class="code-voice">push</code> and <code class="code-voice">pop</code>, to push and pop values on and off the stack. These methods are marked as <code class="code-voice">mutating</code>, because they need to modify (or <em>mutate</em>) the structure’s <code class="code-voice">items</code> array.
</p><p class="para">
The <code class="code-voice">IntStack</code> type shown above can only be used with <code class="code-voice">Int</code> values, however. It would be much more useful to define a <em>generic</em> <code class="code-voice">Stack</code> class, that can manage a stack of <em>any</em> type of value.
</p><p class="para">
Here’s a generic version of the same 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">struct</span> <span class="vc">Stack</span><<span class="vc">T</span>> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">items</span> = <span class="vc">T</span>[]()</code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">push</span>(<span class="vc">item</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="vc">items</span>.<span class="vc">append</span>(<span class="vc">item</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">pop</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">items</span>.<span class="vc">removeLast</span>()</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
Note how the generic version of <code class="code-voice">Stack</code> is essentially the same as the non-generic version, but with a placeholder type parameter called <code class="code-voice">T</code> instead of an actual type of <code class="code-voice">Int</code>. This type parameter is written within a pair of angle brackets (<code class="code-voice"><T></code>) immediately after the structure’s name.
</p><p class="para">
<code class="code-voice">T</code> defines a placeholder name for “some type <code class="code-voice">T</code>” to be provided later on. This future type can be referred to as “<code class="code-voice">T</code>” anywhere within the structure’s definition. In this case, <code class="code-voice">T</code> is used as a placeholder in three places:
</p><ul class="list-bullet">
<li class="item"><p class="para">
To create a property called <code class="code-voice">items</code>, which is initialized with an empty array of values of type <code class="code-voice">T</code>
</p>
</li><li class="item"><p class="para">
To specify that the <code class="code-voice">push</code> method has a single parameter called <code class="code-voice">item</code>, which must be of type <code class="code-voice">T</code>
</p>
</li><li class="item"><p class="para">
To specify that the value returned by the <code class="code-voice">pop</code> method will be a value of type <code class="code-voice">T</code>
</p>
</li>
</ul><p class="para">
You create instances of <code class="code-voice">Stack</code> in a similar way to <code class="code-voice">Array</code> and <code class="code-voice">Dictionary</code>, by writing the actual type to be used for this specific stack within angle brackets after the type name when creating a new instance with initializer syntax:
</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">var</span> <span class="vc">stackOfStrings</span> = <span class="vc">Stack</span><<span class="n"></span>>()</code></li>
<li><code class="code-voice"><span class="vc">stackOfStrings</span>.<span class="vc">push</span>(<span class="s">"uno"</span>)</code></li>
<li><code class="code-voice"><span class="vc">stackOfStrings</span>.<span class="vc">push</span>(<span class="s">"dos"</span>)</code></li>
<li><code class="code-voice"><span class="vc">stackOfStrings</span>.<span class="vc">push</span>(<span class="s">"tres"</span>)</code></li>
<li><code class="code-voice"><span class="vc">stackOfStrings</span>.<span class="vc">push</span>(<span class="s">"cuatro"</span>)</code></li>
<li><code class="code-voice"><span class="c">// the stack now contains 4 strings</span></code></li>
</ul>
</div>
</section><p class="para">
Here’s how <code class="code-voice">stackOfStrings</code> looks after pushing these four values on to the stack:
</p><figure class="figure">
<span class="caption"></span>
<img src="./resource/stackPushedFourStrings_2x.png" alt="image: ../Art/stackPushedFourStrings_2x.png" width="664" height="218">
</figure><p class="para">
Popping a value from the stack returns and removes the top value, <code class="code-voice">"cuatro"</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">fromTheTop</span> = <span class="vc">stackOfStrings</span>.<span class="vc">pop</span>()</code></li>
<li><code class="code-voice"><span class="c">// fromTheTop is equal to "cuatro", and the stack now contains 3 strings</span></code></li>
</ul>
</div>
</section><p class="para">
Here’s how the stack looks after popping its top value:
</p><figure class="figure">
<span class="caption"></span>
<img src="./resource/stackPoppedOneString_2x.png" alt="image: ../Art/stackPoppedOneString_2x.png" width="405" height="207">
</figure><p class="para">
Because it is a generic type, <code class="code-voice">Stack</code> can be used to create a stack of <em>any</em> valid type in Swift, in a similar manner to <code class="code-voice">Array</code> and <code class="code-voice">Dictionary</code>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_244"></a>
<h3 class="section-name" tabindex="0">Type Constraints</h3>
<p class="para">
The <code class="code-voice">swapTwoValues</code> function and the <code class="code-voice">Stack</code> type can work with any type. However, it is sometimes useful to enforce certain <em>type constraints</em> on the types that can be used with generic functions and generic types. Type constraints specify that a type parameter must inherit from a specific class, or conform to a particular protocol or protocol composition.
</p><p class="para">
For example, Swift’s <code class="code-voice">Dictionary</code> type places a limitation on the types that can be used as keys for a dictionary. As described in <span class="x-name"><a href="Collection Types.html#TP40014097-CH8-XID_143" data-id="TP40014097-CH8-XID_143">Dictionaries</a></span>, the type of a dictionary’s keys must be <em>hashable</em>. That is, it must provide a way to make itself uniquely representable. <code class="code-voice">Dictionary</code> needs its keys to be hashable so that it can check whether it already contains a value for a particular key. Without this requirement, <code class="code-voice">Dictionary</code> could not tell whether it should insert or replace a value for a particular key, nor would it be able to find a value for a given key that is already in the dictionary.
</p><p class="para">
This requirement is enforced by a type constraint on the key type for <code class="code-voice">Dictionary</code>, which specifies that the key type must conform to the <code class="code-voice">Hashable</code> protocol, a special protocol defined in the Swift standard library. All of Swift’s basic types (such as <code class="code-voice">String</code>, <code class="code-voice">Int</code>, <code class="code-voice">Double</code>, and <code class="code-voice">Bool</code>) are hashable by default.
</p><p class="para">
You can define your own type constraints when creating custom generic types, and these constraints provide much of the power of generic programming. Abstract concepts like <code class="code-voice">Hashable</code> characterize types in terms of their conceptual characteristics, rather than their explicit type.
</p>
<section class="section">
<a name="TP40014097-CH26-XID_245"></a>
<h3 class="section-name" tabindex="0">Type Constraint Syntax</h3>
<p class="para">
You write type constraints by placing a single class or protocol constraint after a type parameter’s name, separated by a colon, as part of the type parameter list. The basic syntax for type constraints on a generic function is shown below (although the syntax is the same for generic types):
</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">func</span> <span class="vc">someFunction</span><<span class="vc">T</span>: <span class="vc">SomeClass</span>, <span class="vc">U</span>: <span class="vc">SomeProtocol</span>>(<span class="vc">someT</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="c">// function body goes here</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The hypothetical function above has two type parameters. The first type parameter, <code class="code-voice">T</code>, has a type constraint that requires <code class="code-voice">T</code> to be a subclass of <code class="code-voice">SomeClass</code>. The second type parameter, <code class="code-voice">U</code>, has a type constraint that requires <code class="code-voice">U</code> to conform to the protocol <code class="code-voice">SomeProtocol</code>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_246"></a>
<h3 class="section-name" tabindex="0">Type Constraints in Action</h3>
<p class="para">
Here’s a non-generic function called <code class="code-voice">findStringIndex</code>, which is given a <code class="code-voice">String</code> value to find and an array of <code class="code-voice">String</code> values within which to find it. The <code class="code-voice">findStringIndex</code> function returns an optional <code class="code-voice">Int</code> value, which will be the index of the first matching string in the array if it is found, or <code class="code-voice">nil</code> if the string cannot be found:
</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">func</span> <span class="vc">findStringIndex</span>(<span class="vc">array</span>: <span class="n"></span>[], <span class="vc">valueToFind</span>: <span class="n"></span>? {</code></li>
<li><code class="code-voice"> <span class="kt">for</span> (<span class="vc">index</span>, <span class="vc">value</span>) <span class="kt">in</span> <span class="vc">enumerate</span>(<span class="vc">array</span>) {</code></li>
<li><code class="code-voice"> <span class="kt">if</span> <span class="vc">value</span> == <span class="vc">valueToFind</span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">index</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">nil</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">findStringIndex</code> function can be used to find a string value in an array of strings:
</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">strings</span> = [<span class="s">"cat"</span>, <span class="s">"dog"</span>, <span class="s">"llama"</span>, <span class="s">"parakeet"</span>, <span class="s">"terrapin"</span>]</code></li>
<li><code class="code-voice"><span class="kt">if</span> <span class="kt">let</span> <span class="vc">foundIndex</span> = <span class="vc">findStringIndex</span>(<span class="vc">strings</span>, <span class="s">"llama"</span>) {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The index of llama is </span>\(<span class="vc">foundIndex</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "The index of llama is 2"</span></code></li>
</ul>
</div>
</section><p class="para">
The principle of finding the index of a value in an array isn’t useful only for strings, however. You can write the same functionality as a generic function called <code class="code-voice">findIndex</code>, by replacing any mention of strings with values of some type <code class="code-voice">T</code> instead.
</p><p class="para">
Here’s how you might expect a generic version of <code class="code-voice">findStringIndex</code>, called <code class="code-voice">findIndex</code>, to be written. Note that the return type of this function is still <code class="code-voice">Int?</code>, because the function returns an optional index number, not an optional value from the array. Be warned, though—this function does not compile, for reasons explained after the example:
</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">func</span> <span class="vc">findIndex</span><<span class="vc">T</span>>(<span class="vc">array</span>: <span class="n"></span>[], <span class="vc">valueToFind</span>: <span class="n"></span>? {</code></li>
<li><code class="code-voice"> <span class="kt">for</span> (<span class="vc">index</span>, <span class="vc">value</span>) <span class="kt">in</span> <span class="vc">enumerate</span>(<span class="vc">array</span>) {</code></li>
<li><code class="code-voice"> <span class="kt">if</span> <span class="vc">value</span> == <span class="vc">valueToFind</span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">index</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">nil</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
This function does not compile as written above. The problem lies with the equality check, “<code class="code-voice">if value == valueToFind</code>”. Not every type in Swift can be compared with the equal to operator (<code class="code-voice">==</code>). If you create your own class or structure to represent a complex data model, for example, then the meaning of “equal to” for that class or structure is not something that Swift can guess for you. Because of this, it is not possible to guarantee that this code will work for <em>every</em> possible type <code class="code-voice">T</code>, and an appropriate error is reported when you try to compile the code.
</p><p class="para">
All is not lost, however. The Swift standard library defines a protocol called <code class="code-voice">Equatable</code>, which requires any conforming type to implement the equal to operator (<code class="code-voice">==</code>) and the not equal to operator (<code class="code-voice">!=</code>) to compare any two values of that type. All of Swift’s standard types automatically support the <code class="code-voice">Equatable</code> protocol.
</p><p class="para">
Any type that is <code class="code-voice">Equatable</code> can be used safely with the <code class="code-voice">findIndex</code> function, because it is guaranteed to support the equal to operator. To express this fact, you write a type constraint of <code class="code-voice">Equatable</code> as part of the type parameter’s definition when you define the function:
</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">func</span> <span class="vc">findIndex</span><<span class="vc">T</span>: <span class="vc">Equatable</span>>(<span class="vc">array</span>: <span class="n"></span>[], <span class="vc">valueToFind</span>: <span class="n"></span>? {</code></li>
<li><code class="code-voice"> <span class="kt">for</span> (<span class="vc">index</span>, <span class="vc">value</span>) <span class="kt">in</span> <span class="vc">enumerate</span>(<span class="vc">array</span>) {</code></li>
<li><code class="code-voice"> <span class="kt">if</span> <span class="vc">value</span> == <span class="vc">valueToFind</span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">index</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">nil</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The single type parameter for <code class="code-voice">findIndex</code> is written as <code class="code-voice">T: Equatable</code>, which means “any type <code class="code-voice">T</code> that conforms to the <code class="code-voice">Equatable</code> protocol.”
</p><p class="para">
The <code class="code-voice">findIndex</code> function now compiles successfully and can be used with any type that is <code class="code-voice">Equatable</code>, such as <code class="code-voice">Double</code> or <code class="code-voice">String</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">doubleIndex</span> = <span class="vc">findIndex</span>([<span class="m">3.14159</span>, <span class="m">0.1</span>, <span class="m">0.25</span>], <span class="m">9.3</span>)</code></li>
<li><code class="code-voice"><span class="c">// doubleIndex is an optional Int with no value, because 9.3 is not in the array</span></code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">stringIndex</span> = <span class="vc">findIndex</span>([<span class="s">"Mike"</span>, <span class="s">"Malcolm"</span>, <span class="s">"Andrea"</span>], <span class="s">"Andrea"</span>)</code></li>
<li><code class="code-voice"><span class="c">// stringIndex is an optional Int containing a value of 2</span></code></li>
</ul>
</div>
</section>
</section>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_247"></a>
<h3 class="section-name" tabindex="0">Associated Types</h3>
<p class="para">
When defining a protocol, it is sometimes useful to declare one or more <em>associated types</em> as part of the protocol’s definition. An associated type gives a placeholder name (or <em>alias</em>) to a type that is used as part of the protocol. The actual type to use for that associated type is not specified until the protocol is adopted. Associated types are specified with the <code class="code-voice">typealias</code> keyword.
</p>
<section class="section">
<a name="TP40014097-CH26-XID_248"></a>
<h3 class="section-name" tabindex="0">Associated Types in Action</h3>
<p class="para">
Here’s an example of a protocol called <code class="code-voice">Container</code>, which declares an associated type called <code class="code-voice">ItemType</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">protocol</span> <span class="vc">Container</span> {</code></li>
<li><code class="code-voice"> <span class="kt">typealias</span> <span class="vc">ItemType</span></code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">append</span>(<span class="vc">item</span>: <span class="n"></span>)</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">count</span>: <span class="n"></span> { <span class="kt">get</span> }</code></li>
<li><code class="code-voice"> <span class="kt">subscript</span>(<span class="vc">i</span>: <span class="n"></span> { <span class="kt">get</span> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">Container</code> protocol defines three required capabilities that any container must provide:
</p><ul class="list-bullet">
<li class="item"><p class="para">
It must be possible to add a new item to the container with an <code class="code-voice">append</code> method.
</p>
</li><li class="item"><p class="para">
It must be possible to access a count of the items in the container through a <code class="code-voice">count</code> property that returns an <code class="code-voice">Int</code> value.
</p>
</li><li class="item"><p class="para">
It must be possible to retrieve each item in the container with a subscript that takes an <code class="code-voice">Int</code> index value.
</p>
</li>
</ul><p class="para">
This protocol doesn’t specify how the items in the container should be stored or what type they are allowed to be. The protocol only specifies the three bits of functionality that any type must provide in order to be considered a <code class="code-voice">Container</code>. A conforming type can provide additional functionality, as long as it satisfies these three requirements.
</p><p class="para">
Any type that conforms to the <code class="code-voice">Container</code> protocol must be able to specify the type of values it stores. Specifically, it must ensure that only items of the right type are added to the container, and it must be clear about the type of the items returned by its subscript.
</p><p class="para">
To define these requirements, the <code class="code-voice">Container</code> protocol needs a way to refer to the type of the elements that a container will hold, without knowing what that type is for a specific container. The <code class="code-voice">Container</code> protocol needs to specify that any value passed to the <code class="code-voice">append</code> method must have the same type as the container’s element type, and that the value returned by the container’s subscript will be of the same type as the container’s element type.
</p><p class="para">
To achieve this, the <code class="code-voice">Container</code> protocol declares an associated type called <code class="code-voice">ItemType</code>, written as <code class="code-voice">typealias ItemType</code>. The protocol does not define what <code class="code-voice">ItemType</code> is an alias <em>for</em>—that information is left for any conforming type to provide. Nonetheless, the <code class="code-voice">ItemType</code> alias provides a way to refer to the type of the items in a <code class="code-voice">Container</code>, and to define a type for use with the <code class="code-voice">append</code> method and subscript, to ensure that the expected behavior of any <code class="code-voice">Container</code> is enforced.
</p><p class="para">
Here’s a version of the non-generic <code class="code-voice">IntStack</code> type from earlier, adapted to conform to the <code class="code-voice">Container</code> protocol:
</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">IntStack</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="c">// original IntStack implementation</span></code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">items</span> = <span class="vc">Int</span>[]()</code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">push</span>(<span class="vc">item</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="vc">items</span>.<span class="vc">append</span>(<span class="vc">item</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">pop</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">items</span>.<span class="vc">removeLast</span>()</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="c">// conformance to the Container protocol</span></code></li>
<li><code class="code-voice"> <span class="kt">typealias</span> <span class="vc">ItemType</span> = <span class="n"></span></code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">append</span>(<span class="vc">item</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="kt">self</span>.<span class="vc">push</span>(<span class="vc">item</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">count</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">items</span>.<span class="vc">count</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">subscript</span>(<span class="vc">i</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">items</span>[<span class="vc">i</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">IntStack</code> type implements all three of the <code class="code-voice">Container</code> protocol’s requirements, and in each case wraps part of the <code class="code-voice">IntStack</code> type’s existing functionality to satisfy these requirements.
</p><p class="para">
Moreover, <code class="code-voice">IntStack</code> specifies that for this implementation of <code class="code-voice">Container</code>, the appropriate <code class="code-voice">ItemType</code> to use is a type of <code class="code-voice">Int</code>. The definition of <code class="code-voice">typealias ItemType = Int</code> turns the abstract type of <code class="code-voice">ItemType</code> into a concrete type of <code class="code-voice">Int</code> for this implementation of the <code class="code-voice">Container</code> protocol.
</p><p class="para">
Thanks to Swift’s type inference, you don’t actually need to declare a concrete <code class="code-voice">ItemType</code> of <code class="code-voice">Int</code> as part of the definition of <code class="code-voice">IntStack</code>. Because <code class="code-voice">IntStack</code> conforms to all of the requirements of the <code class="code-voice">Container</code> protocol, Swift can infer the appropriate <code class="code-voice">ItemType</code> to use, simply by looking at the type of the <code class="code-voice">append</code> method’s <code class="code-voice">item</code> parameter and the return type of the subscript. Indeed, if you delete the <code class="code-voice">typealias ItemType = Int</code> line from the code above, everything still works, because it is clear what type should be used for <code class="code-voice">ItemType</code>.
</p><p class="para">
You can also make the generic <code class="code-voice">Stack</code> type conform to the <code class="code-voice">Container</code> protocol:
</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">Stack</span><<span class="vc">T</span>>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="c">// original Stack<T> implementation</span></code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">items</span> = <span class="vc">T</span>[]()</code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">push</span>(<span class="vc">item</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="vc">items</span>.<span class="vc">append</span>(<span class="vc">item</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">pop</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">items</span>.<span class="vc">removeLast</span>()</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="c">// conformance to the Container protocol</span></code></li>
<li><code class="code-voice"> <span class="kt">mutating</span> <span class="kt">func</span> <span class="vc">append</span>(<span class="vc">item</span>: <span class="n"></span>) {</code></li>
<li><code class="code-voice"> <span class="kt">self</span>.<span class="vc">push</span>(<span class="vc">item</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">count</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">items</span>.<span class="vc">count</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">subscript</span>(<span class="vc">i</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">items</span>[<span class="vc">i</span>]</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
This time, the placeholder type parameter <code class="code-voice">T</code> is used as the type of the <code class="code-voice">append</code> method’s <code class="code-voice">item</code> parameter and the return type of the subscript. Swift can therefore infer that <code class="code-voice">T</code> is the appropriate type to use as the <code class="code-voice">ItemType</code> for this particular container.
</p>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_249"></a>
<h3 class="section-name" tabindex="0">Extending an Existing Type to Specify an Associated Type</h3>
<p class="para">
You can extend an existing type to add conformance to a protocol, as described in <span class="x-name"><a href="Protocols.html#TP40014097-CH25-XID_355" data-id="TP40014097-CH25-XID_355">Adding Protocol Conformance with an Extension</a></span>. This includes a protocol with an associated type.
</p><p class="para">
Swift’s <code class="code-voice">Array</code> type already provides an <code class="code-voice">append</code> method, a <code class="code-voice">count</code> property, and a subscript with an <code class="code-voice">Int</code> index to retrieve its elements. These three capabilities match the requirements of the <code class="code-voice">Container</code> protocol. This means that you can extend <code class="code-voice">Array</code> to conform to the <code class="code-voice">Container</code> protocol simply by declaring that <code class="code-voice">Array</code> adopts the protocol. You do this with an empty extension, as described in <span class="x-name"><a href="Protocols.html#TP40014097-CH25-XID_357" data-id="TP40014097-CH25-XID_357">Declaring Protocol Adoption with an Extension</a></span>:
</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">extension</span> <span class="n"></span> {}</code></li>
</ul>
</div>
</section><p class="para">
Array’s existing <code class="code-voice">append</code> method and subscript enable Swift to infer the appropriate type to use for <code class="code-voice">ItemType</code>, just as for the generic <code class="code-voice">Stack</code> type above. After defining this extension, you can use any <code class="code-voice">Array</code> as a <code class="code-voice">Container</code>.
</p>
</section>
</section>
<section class="section">
<a name="TP40014097-CH26-XID_250"></a>
<h3 class="section-name" tabindex="0">Where Clauses</h3>
<p class="para">
Type constraints, as described in <span class="x-name"><a href="#TP40014097-CH26-XID_244" data-id="TP40014097-CH26-XID_244">Type Constraints</a></span>, enable you to define requirements on the type parameters associated with a generic function or type.
</p><p class="para">
It can also be useful to define requirements for associated types. You do this by defining <em>where clauses</em> as part of a type parameter list. A where clause enables you to require that an associated type conforms to a certain protocol, and/or that certain type parameters and associated types be the same. You write a where clause by placing the <code class="code-voice">where</code> keyword immediately after the list of type parameters, followed by one or more constraints for associated types, and/or one or more equality relationships between types and associated types.
</p><p class="para">
The example below defines a generic function called <code class="code-voice">allItemsMatch</code>, which checks to see if two <code class="code-voice">Container</code> instances contain the same items in the same order. The function returns a Boolean value of <code class="code-voice">true</code> if all items match and a value of <code class="code-voice">false</code> if they do not.
</p><p class="para">
The two containers to be checked do not have to be the same type of container (although they can be), but they do have to hold the same type of items. This requirement is expressed through a combination of type constraints and where clauses:
</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">func</span> <span class="vc">allItemsMatch</span><</code></li>
<li><code class="code-voice"> <span class="vc">C1</span>: <span class="vc">Container</span>, <span class="vc">C2</span>: <span class="vc">Container</span></code></li>
<li><code class="code-voice"> <span class="kt">where</span> <span class="vc">C1</span>.<span class="vc">ItemType</span> == <span class="vc">C2</span>.<span class="vc">ItemType</span>, <span class="vc">C1</span>.<span class="vc">ItemType</span>: <span class="vc">Equatable</span>></code></li>
<li><code class="code-voice"> (<span class="vc">someContainer</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"> <span class="c">// check that both containers contain the same number of items</span></code></li>
<li><code class="code-voice"> <span class="kt">if</span> <span class="vc">someContainer</span>.<span class="vc">count</span> != <span class="vc">anotherContainer</span>.<span class="vc">count</span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">false</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">// check each pair of items to see if they are equivalent</span></code></li>
<li><code class="code-voice"> <span class="kt">for</span> <span class="vc">i</span> <span class="kt">in</span> <span class="m">0</span>..<span class="vc">someContainer</span>.<span class="vc">count</span> {</code></li>
<li><code class="code-voice"> <span class="kt">if</span> <span class="vc">someContainer</span>[<span class="vc">i</span>] != <span class="vc">anotherContainer</span>[<span class="vc">i</span>] {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">false</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"> <span class="c">// all items match, so return true</span></code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">true</span></code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
This function takes two arguments called <code class="code-voice">someContainer</code> and <code class="code-voice">anotherContainer</code>. The <code class="code-voice">someContainer</code> argument is of type <code class="code-voice">C1</code>, and the <code class="code-voice">anotherContainer</code> argument is of type <code class="code-voice">C2</code>. Both <code class="code-voice">C1</code> and <code class="code-voice">C2</code> are placeholder type parameters for two container types to be determined when the function is called.
</p><p class="para">
The function’s type parameter list places the following requirements on the two type parameters:
</p><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">C1</code> must conform to the <code class="code-voice">Container</code> protocol (written as <code class="code-voice">C1: Container</code>).
</p>
</li><li class="item"><p class="para">
<code class="code-voice">C2</code> must also conform to the <code class="code-voice">Container</code> protocol (written as <code class="code-voice">C2: Container</code>).
</p>
</li><li class="item"><p class="para">
The <code class="code-voice">ItemType</code> for <code class="code-voice">C1</code> must be the same as the <code class="code-voice">ItemType</code> for <code class="code-voice">C2</code> (written as <code class="code-voice">C1.ItemType == C2.ItemType</code>).
</p>
</li><li class="item"><p class="para">
The <code class="code-voice">ItemType</code> for <code class="code-voice">C1</code> must conform to the <code class="code-voice">Equatable</code> protocol (written as <code class="code-voice">C1.ItemType: Equatable</code>).
</p>
</li>
</ul><p class="para">
The third and fourth requirements are defined as part of a where clause, and are written after the <code class="code-voice">where</code> keyword as part of the function’s type parameter list.
</p><p class="para">
These requirements mean:
</p><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">someContainer</code> is a container of type <code class="code-voice">C1</code>.
</p>
</li><li class="item"><p class="para">
<code class="code-voice">anotherContainer</code> is a container of type <code class="code-voice">C2</code>.
</p>
</li><li class="item"><p class="para">
<code class="code-voice">someContainer</code> and <code class="code-voice">anotherContainer</code> contain the same type of items.
</p>
</li><li class="item"><p class="para">
The items in <code class="code-voice">someContainer</code> can be checked with the not equal operator (<code class="code-voice">!=</code>) to see if they are different from each other.
</p>
</li>
</ul><p class="para">
The third and fourth requirements combine to mean that the items in <code class="code-voice">anotherContainer</code> can <em>also</em> be checked with the <code class="code-voice">!=</code> operator, because they are exactly the same type as the items in <code class="code-voice">someContainer</code>.
</p><p class="para">
These requirements enable the <code class="code-voice">allItemsMatch</code> function to compare the two containers, even if they are of a different container type.
</p><p class="para">
The <code class="code-voice">allItemsMatch</code> function starts by checking that both containers contain the same number of items. If they contain a different number of items, there is no way that they can match, and the function returns <code class="code-voice">false</code>.
</p><p class="para">
After making this check, the function iterates over all of the items in <code class="code-voice">someContainer</code> with a <code class="code-voice">for</code>-<code class="code-voice">in</code> loop and the half-closed range operator (<code class="code-voice">..</code>). For each item, the function checks whether the item from <code class="code-voice">someContainer</code> is not equal to the corresponding item in <code class="code-voice">anotherContainer</code>. If the two items are not equal, then the two containers do not match, and the function returns <code class="code-voice">false</code>.
</p><p class="para">
If the loop finishes without finding a mismatch, the two containers match, and the function returns <code class="code-voice">true</code>.
</p><p class="para">
Here’s how the <code class="code-voice">allItemsMatch</code> function looks in action:
</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">var</span> <span class="vc">stackOfStrings</span> = <span class="vc">Stack</span><<span class="n"></span>>()</code></li>
<li><code class="code-voice"><span class="vc">stackOfStrings</span>.<span class="vc">push</span>(<span class="s">"uno"</span>)</code></li>
<li><code class="code-voice"><span class="vc">stackOfStrings</span>.<span class="vc">push</span>(<span class="s">"dos"</span>)</code></li>
<li><code class="code-voice"><span class="vc">stackOfStrings</span>.<span class="vc">push</span>(<span class="s">"tres"</span>)</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">arrayOfStrings</span> = [<span class="s">"uno"</span>, <span class="s">"dos"</span>, <span class="s">"tres"</span>]</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="kt">if</span> <span class="vc">allItemsMatch</span>(<span class="vc">stackOfStrings</span>, <span class="vc">arrayOfStrings</span>) {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"All items match."</span>)</code></li>
<li><code class="code-voice">} <span class="kt">else</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"Not all items match."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "All items match."</span></code></li>
</ul>
</div>
</section><p class="para">
The example above creates a <code class="code-voice">Stack</code> instance to store <code class="code-voice">String</code> values, and pushes three strings onto the stack. The example also creates an <code class="code-voice">Array</code> instance initialized with an array literal containing the same three strings as the stack. Even though the stack and the array are of a different type, they both conform to the <code class="code-voice">Container</code> protocol, and both contain the same type of values. You can therefore call the <code class="code-voice">allItemsMatch</code> function with these two containers as its arguments. In the example above, the <code class="code-voice">allItemsMatch</code> function correctly reports that all of the items in the two containers match.
</p>
</section>
<section id="next_previous" class="">
<p class="previous-link"><a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345">Protocols</a></p>
<p class="next-link"><a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28">Advanced Operators</a></p>