-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection Types.html
More file actions
992 lines (775 loc) · 60.8 KB
/
Copy pathCollection Types.html
File metadata and controls
992 lines (775 loc) · 60.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
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Swift Programming Language: Collection Types</title>
<link rel="stylesheet" type="text/css" href="resource/style-1.1.15.css">
<meta charset="utf-8"> <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
<body id="conceptual_flow_with_tasks" class="jazz">
<div id="_omniture_top">
</div>
<a name="TP40014097" title="The Swift Programming Language"></a>
<section id="valence">
<div class="content-wrapper">
<p id="hierarchial_navigation">
<span id="book_title">The Swift Programming Language</span>
</p>
<img id="shortstack" src="./resource/shortstack_2x.png">
</div>
</section>
<div class="content-wrapper">
<nav class="book-parts hideInXcode" role="navigation">
<ul class="nav-parts">
<li data-id="TP40014097-CH1-XID_27" class="part-name">Welcome to Swift
<ul class="nav-chapters">
<li class="nav-chapter nav-visited-chapter">
<a href="About Swift.html#TP40014097-CH3-XID_0" data-id="TP40014097-CH3-XID_0" class="">About Swift</a>
</li>
<li class="nav-chapter">
<a href="A Swift Tour.html#TP40014097-CH2-XID_1" data-id="TP40014097-CH2-XID_1" class="">A Swift Tour</a>
</li>
</ul>
</li><li data-id="TP40014097-CH4-XID_299" class="part-name nav-part-active open-part">Language Guide
<ul class="nav-chapters" style="height: 607px;">
<li class="nav-chapter">
<a href="The Basics.html" data-id="TP40014097-CH5-XID_399" >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">Strings and Characters</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133" class="nav-chapter-active">Collection Types</a>
</li>
<li class="nav-chapter">
<a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153">Control Flow</a>
</li>
<li class="nav-chapter">
<a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204">Functions</a>
</li>
<li class="nav-chapter">
<a href="Closures.html#TP40014097-CH11-XID_117" data-id="TP40014097-CH11-XID_117">Closures</a>
</li>
<li class="nav-chapter">
<a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185">Enumerations</a>
</li>
<li class="nav-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94">Classes and Structures</a>
</li>
<li class="nav-chapter">
<a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323">Properties</a>
</li>
<li class="nav-chapter">
<a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300">Methods</a>
</li>
<li class="nav-chapter">
<a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393">Subscripts</a>
</li>
<li class="nav-chapter">
<a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251">Inheritance</a>
</li>
<li class="nav-chapter">
<a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a>
</li>
<li class="nav-chapter">
<a href="Deinitialization.html#TP40014097-CH19-XID_182" data-id="TP40014097-CH19-XID_182">Deinitialization</a>
</li>
<li class="nav-chapter">
<a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50">Automatic Reference Counting</a>
</li>
<li class="nav-chapter">
<a href="Optional Chaining.html#TP40014097-CH21-XID_312" data-id="TP40014097-CH21-XID_312">Optional Chaining</a>
</li>
<li class="nav-chapter">
<a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443">Type Casting</a>
</li>
<li class="nav-chapter">
<a href="Nested Types.html#TP40014097-CH23-XID_309" data-id="TP40014097-CH23-XID_309">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name">Language Reference
<ul class="nav-chapters">
<li class="nav-chapter">
<a href="About the Language Reference.html#TP40014097-CH29-XID_453" data-id="TP40014097-CH29-XID_453" class="">About the Language Reference</a>
</li>
<li class="nav-chapter">
<a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794">Lexical Structure</a>
</li>
<li class="nav-chapter">
<a href="Types.html#TP40014097-CH31-XID_988" data-id="TP40014097-CH31-XID_988">Types</a>
</li>
<li class="nav-chapter">
<a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655">Expressions</a>
</li>
<li class="nav-chapter">
<a href="Statements.html#TP40014097-CH33-XID_913" data-id="TP40014097-CH33-XID_913">Statements</a>
</li>
<li class="nav-chapter">
<a href="Declarations.html#TP40014097-CH34-XID_475" data-id="TP40014097-CH34-XID_475">Declarations</a>
</li>
<li class="nav-chapter">
<a href="Attributes.html#TP40014097-CH35-XID_460" data-id="TP40014097-CH35-XID_460">Attributes</a>
</li>
<li class="nav-chapter">
<a href="Patterns.html#TP40014097-CH36-XID_878" data-id="TP40014097-CH36-XID_878">Patterns</a>
</li>
<li class="nav-chapter">
<a href="Generic Parameters and Arguments.html#TP40014097-CH37-XID_774" data-id="TP40014097-CH37-XID_774">Generic Parameters and Arguments</a>
</li>
<li class="nav-chapter">
<a href="Summary of the Grammar.html#TP40014097-CH38-XID_1030" data-id="TP40014097-CH38-XID_1030">Summary of the Grammar</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="chapter">
<a name="TP40014097-CH8"></a><a name="TP40014097-CH8-XID_133"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Collection Types</h2>
<section id="mini_toc" class="hideInXcode" role="navigation">
<div id="mini_toc_button">
<p>On This Page</p>
</div>
<ul class="list-bullet">
<li class="item">
<p class="para">
<a href="#TP40014097-CH8-XID_135">
Arrays
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH8-XID_143">
Dictionaries
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH8-XID_151">
Mutability of Collections
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
Swift provides two <em>collection types</em>, known as arrays and dictionaries, for storing collections of values. Arrays store ordered lists of values of the same type. Dictionaries store unordered collections of values of the same type, which can be referenced and looked up through a unique identifier (also known as a key).
</p>
<p class="para">
Arrays and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you cannot insert a value of the wrong type into an array or dictionary by mistake. It also means you can be confident about the types of values you will retrieve from an array or dictionary. Swift’s use of explicitly typed collections ensures that your code is always clear about the types of values it can work with and enables you to catch any type mismatches early in your code’s development.
</p>
<div class="note">
<a name="TP40014097-CH8-XID_134"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Swift’s <code class="code-voice">Array</code> type exhibits different behavior to other types when assigned to a constant or variable, or when passed to a function or method. For more information, see <span class="x-name"><a href="#TP40014097-CH8-XID_151" data-id="TP40014097-CH8-XID_151">Mutability of Collections</a></span> and <span class="x-name"><a href="Classes and Structures.html#TP40014097-CH13-XID_109" data-id="TP40014097-CH13-XID_109">Assignment and Copy Behavior for Collection Types</a></span>.
</p>
</aside>
</div>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_135"></a>
<h3 class="section-name" tabindex="0">Arrays</h3>
<p class="para">
An <em>array</em> stores multiple values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.
</p><p class="para">
Swift arrays are specific about the kinds of values they can store. They differ from Objective-C’s <code class="code-voice">NSArray</code> and <code class="code-voice">NSMutableArray</code> classes, which can store any kind of object and do not provide any information about the nature of the objects they return. In Swift, the type of values that a particular array can store is always made clear, either through an explicit type annotation, or through type inference, and does not have to be a class type. If you create an array of <code class="code-voice">Int</code> values, for example, you can’t insert any value other than <code class="code-voice">Int</code> values into that array. Swift arrays are type safe, and are always clear about what they may contain.
</p>
<section class="section">
<a name="TP40014097-CH8-XID_136"></a>
<h3 class="section-name" tabindex="0">Array Type Shorthand Syntax</h3>
<p class="para">
The type of a Swift array is written in full as <code class="code-voice">Array<SomeType></code>, where <code class="code-voice">SomeType</code> is the type that the array is allowed to store. You can also write the type of an array in shorthand form as <code class="code-voice">SomeType[]</code>. Although the two forms are functionally identical, the shorthand form is preferred, and is used throughout this guide when referring to the type of an array.
</p>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_137"></a>
<h3 class="section-name" tabindex="0">Array Literals</h3>
<p class="para">
You can initialize an array with an <em>array literal</em>, which is a shorthand way to write one or more values as an array collection. An array literal is written as a list of values, separated by commas, surrounded by a pair of square brackets:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice">[<em class="variable-text">value 1</em>, <em class="variable-text">value 2</em>, <em class="variable-text">value 3</em>]</pre></li>
</ul>
</div><p class="para">
The example below creates an array called <code class="code-voice">shoppingList</code> to store <code class="code-voice">String</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">var</span> <span class="vc">shoppingList</span>: <span class="n"></span>[] = [<span class="s">"Eggs"</span>, <span class="s">"Milk"</span>]</code></li>
<li><code class="code-voice"><span class="c">// shoppingList has been initialized with two initial items</span></code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">shoppingList</code> variable is declared as “an array of <code class="code-voice">String</code> values”, written as <code class="code-voice">String[]</code>. Because this particular array has specified a value type of <code class="code-voice">String</code>, it is <em>only</em> allowed to store <code class="code-voice">String</code> values. Here, the <code class="code-voice">shoppingList</code> array is initialized with two <code class="code-voice">String</code> values (<code class="code-voice">"Eggs"</code> and <code class="code-voice">"Milk"</code>), written within an array literal.
</p><div class="note">
<a name="TP40014097-CH8-XID_138"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">The <code class="code-voice">shoppingList</code> array is declared as a variable (with the <code class="code-voice">var</code> introducer) and not a constant (with the <code class="code-voice">let</code> introducer) because more items are added to the shopping list in the examples below.
</p>
</aside>
</div><p class="para">
In this case, the array literal contains two <code class="code-voice">String</code> values and nothing else. This matches the type of the <code class="code-voice">shoppingList</code> variable’s declaration (an array that can only contain <code class="code-voice">String</code> values), and so the assignment of the array literal is permitted as a way to initialize <code class="code-voice">shoppingList</code> with two initial items.
</p><p class="para">
Thanks to Swift’s type inference, you don’t have to write the type of the array if you’re initializing it with an array literal containing values of the same type. The initialization of <code class="code-voice">shoppingList</code> could have been written in a shorter form instead:
</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">shoppingList</span> = [<span class="s">"Eggs"</span>, <span class="s">"Milk"</span>]</code></li>
</ul>
</div>
</section><p class="para">
Because all values in the array literal are of the same type, Swift can infer that <code class="code-voice">String[]</code> is the correct type to use for the <code class="code-voice">shoppingList</code> variable.
</p>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_139"></a>
<h3 class="section-name" tabindex="0">Accessing and Modifying an Array</h3>
<p class="para">
You access and modify an array through its methods and properties, or by using subscript syntax.
</p><p class="para">
To find out the number of items in an array, check its read-only <code class="code-voice">count</code> property:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"The shopping list contains </span>\(<span class="vc">shoppingList</span>.<span class="vc">count</span>)<span class="s"> items."</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "The shopping list contains 2 items."</span></code></li>
</ul>
</div>
</section><p class="para">
Use the Boolean <code class="code-voice">isEmpty</code> property as a shortcut for checking whether the <code class="code-voice">count</code> property is equal to <code class="code-voice">0</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">if</span> <span class="vc">shoppingList</span>.<span class="vc">isEmpty</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The shopping list is empty."</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">"The shopping list is not empty."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "The shopping list is not empty."</span></code></li>
</ul>
</div>
</section><p class="para">
You can add a new item to the end of an array by calling the array’s <code class="code-voice">append</code> method:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">shoppingList</span>.<span class="vc">append</span>(<span class="s">"Flour"</span>)</code></li>
<li><code class="code-voice"><span class="c">// shoppingList now contains 3 items, and someone is making pancakes</span></code></li>
</ul>
</div>
</section><p class="para">
Alternatively, add a new item to the end of an array with the addition assignment operator (<code class="code-voice">+=</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="vc">shoppingList</span> += <span class="s">"Baking Powder"</span></code></li>
<li><code class="code-voice"><span class="c">// shoppingList now contains 4 items</span></code></li>
</ul>
</div>
</section><p class="para">
You can also append an array of compatible items with the addition assignment operator (<code class="code-voice">+=</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="vc">shoppingList</span> += [<span class="s">"Chocolate Spread"</span>, <span class="s">"Cheese"</span>, <span class="s">"Butter"</span>]</code></li>
<li><code class="code-voice"><span class="c">// shoppingList now contains 7 items</span></code></li>
</ul>
</div>
</section><p class="para">
Retrieve a value from the array by using <em>subscript syntax</em>, passing the index of the value you want to retrieve within square brackets immediately after the name of the array:
</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">firstItem</span> = <span class="vc">shoppingList</span>[<span class="m">0</span>]</code></li>
<li><code class="code-voice"><span class="c">// firstItem is equal to "Eggs"</span></code></li>
</ul>
</div>
</section><p class="para">
Note that the first item in the array has an index of <code class="code-voice">0</code>, not <code class="code-voice">1</code>. Arrays in Swift are always zero-indexed.
</p><p class="para">
You can use subscript syntax to change an existing value at a given index:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">shoppingList</span>[<span class="m">0</span>] = <span class="s">"Six eggs"</span></code></li>
<li><code class="code-voice"><span class="c">// the first item in the list is now equal to "Six eggs" rather than "Eggs"</span></code></li>
</ul>
</div>
</section><p class="para">
You can also use subscript syntax to change a range of values at once, even if the replacement set of values has a different length than the range you are replacing. The following example replaces <code class="code-voice">"Chocolate Spread"</code>, <code class="code-voice">"Cheese"</code>, and <code class="code-voice">"Butter"</code> with <code class="code-voice">"Bananas"</code> and <code class="code-voice">"Apples"</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="vc">shoppingList</span>[<span class="m">4</span>...<span class="m">6</span>] = [<span class="s">"Bananas"</span>, <span class="s">"Apples"</span>]</code></li>
<li><code class="code-voice"><span class="c">// shoppingList now contains 6 items</span></code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH8-XID_140"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">You can’t use subscript syntax to append a new item to the end of an array. If you try to use subscript syntax to retrieve or set a value for an index that is outside of an array’s existing bounds, you will trigger a runtime error. However, you can check that an index is valid before using it, by comparing it to the array’s <code class="code-voice">count</code> property. Except when <code class="code-voice">count</code> is <code class="code-voice">0</code> (meaning the array is empty), the largest valid index in an array will always be <code class="code-voice">count - 1</code>, because arrays are indexed from zero.
</p>
</aside>
</div><p class="para">
To insert an item into the array at a specified index, call the array’s <code class="code-voice">insert(atIndex:)</code> method:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">shoppingList</span>.<span class="vc">insert</span>(<span class="s">"Maple Syrup"</span>, <span class="vc">atIndex</span>: <span class="m">0</span>)</code></li>
<li><code class="code-voice"><span class="c">// shoppingList now contains 7 items</span></code></li>
<li><code class="code-voice"><span class="c">// "Maple Syrup" is now the first item in the list</span></code></li>
</ul>
</div>
</section><p class="para">
This call to the <code class="code-voice">insert</code> method inserts a new item with a value of <code class="code-voice">"Maple Syrup"</code> at the very beginning of the shopping list, indicated by an index of <code class="code-voice">0</code>.
</p><p class="para">
Similarly, you remove an item from the array with the <code class="code-voice">removeAtIndex</code> method. This method removes the item at the specified index and returns the removed item (although you can ignore the returned value if you do not need it):
</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">mapleSyrup</span> = <span class="vc">shoppingList</span>.<span class="vc">removeAtIndex</span>(<span class="m">0</span>)</code></li>
<li><code class="code-voice"><span class="c">// the item that was at index 0 has just been removed</span></code></li>
<li><code class="code-voice"><span class="c">// shoppingList now contains 6 items, and no Maple Syrup</span></code></li>
<li><code class="code-voice"><span class="c">// the mapleSyrup constant is now equal to the removed "Maple Syrup" string</span></code></li>
</ul>
</div>
</section><p class="para">
Any gaps in an array are closed when an item is removed, and so the value at index <code class="code-voice">0</code> is once again equal to <code class="code-voice">"Six eggs"</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="vc">firstItem</span> = <span class="vc">shoppingList</span>[<span class="m">0</span>]</code></li>
<li><code class="code-voice"><span class="c">// firstItem is now equal to "Six eggs"</span></code></li>
</ul>
</div>
</section><p class="para">
If you want to remove the final item from an array, use the <code class="code-voice">removeLast</code> method rather than the <code class="code-voice">removeAtIndex</code> method to avoid the need to query the array’s <code class="code-voice">count</code> property. Like the <code class="code-voice">removeAtIndex</code> method, <code class="code-voice">removeLast</code> returns the removed item:
</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">apples</span> = <span class="vc">shoppingList</span>.<span class="vc">removeLast</span>()</code></li>
<li><code class="code-voice"><span class="c">// the last item in the array has just been removed</span></code></li>
<li><code class="code-voice"><span class="c">// shoppingList now contains 5 items, and no cheese</span></code></li>
<li><code class="code-voice"><span class="c">// the apples constant is now equal to the removed "Apples" string</span></code></li>
</ul>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_141"></a>
<h3 class="section-name" tabindex="0">Iterating Over an Array</h3>
<p class="para">
You can iterate over the entire set of values in an array with the <code class="code-voice">for</code>-<code class="code-voice">in</code> 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="vc">item</span> <span class="kt">in</span> <span class="vc">shoppingList</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="vc">item</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// Six eggs</span></code></li>
<li><code class="code-voice"><span class="c">// Milk</span></code></li>
<li><code class="code-voice"><span class="c">// Flour</span></code></li>
<li><code class="code-voice"><span class="c">// Baking Powder</span></code></li>
<li><code class="code-voice"><span class="c">// Bananas</span></code></li>
</ul>
</div>
</section><p class="para">
If you need the integer index of each item as well as its value, use the global <code class="code-voice">enumerate</code> function to iterate over the array instead. The <code class="code-voice">enumerate</code> function returns a tuple for each item in the array composed of the index and the value for that item. You can decompose the tuple into temporary constants or variables as part of the iteration:
</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="vc">index</span>, <span class="vc">value</span>) <span class="kt">in</span> <span class="vc">enumerate</span>(<span class="vc">shoppingList</span>) {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"Item </span>\(<span class="vc">index</span> + <span class="m">1</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="c">// Item 1: Six eggs</span></code></li>
<li><code class="code-voice"><span class="c">// Item 2: Milk</span></code></li>
<li><code class="code-voice"><span class="c">// Item 3: Flour</span></code></li>
<li><code class="code-voice"><span class="c">// Item 4: Baking Powder</span></code></li>
<li><code class="code-voice"><span class="c">// Item 5: Bananas</span></code></li>
</ul>
</div>
</section><p class="para">
For more about the <code class="code-voice">for</code>-<code class="code-voice">in</code> loop, see <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_154" data-id="TP40014097-CH9-XID_154">For Loops</a></span>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_142"></a>
<h3 class="section-name" tabindex="0">Creating and Initializing an Array</h3>
<p class="para">
You can create an empty array of a certain type (without setting any initial values) using 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">someInts</span> = <span class="vc">Int</span>[]()</code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"someInts is of type Int[] with </span>\(<span class="vc">someInts</span>.<span class="vc">count</span>)<span class="s"> items."</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "someInts is of type Int[] with 0 items."</span></code></li>
</ul>
</div>
</section><p class="para">
Note that the type of the <code class="code-voice">someInts</code> variable is inferred to be <code class="code-voice">Int[]</code>, because it is set to the output of an <code class="code-voice">Int[]</code> initializer.
</p><p class="para">
Alternatively, if the context already provides type information, such as a function argument or an already-typed variable or constant, you can create an empty array with an empty array literal, which is written as <code class="code-voice">[]</code> (an empty pair of square brackets):
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">someInts</span>.<span class="vc">append</span>(<span class="m">3</span>)</code></li>
<li><code class="code-voice"><span class="c">// someInts now contains 1 value of type Int</span></code></li>
<li><code class="code-voice"><span class="vc">someInts</span> = []</code></li>
<li><code class="code-voice"><span class="c">// someInts is now an empty array, but is still of type Int[]</span></code></li>
</ul>
</div>
</section><p class="para">
Swift’s <code class="code-voice">Array</code> type also provides an initializer for creating an array of a certain size with all of its values set to a provided default value. You pass this initializer the number of items to be added to the new array (called <code class="code-voice">count</code>) and a default value of the appropriate type (called <code class="code-voice">repeatedValue</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">var</span> <span class="vc">threeDoubles</span> = <span class="vc">Double</span>[](<span class="vc">count</span>: <span class="m">3</span>, <span class="vc">repeatedValue</span>: <span class="m">0.0</span>)</code></li>
<li><code class="code-voice"><span class="c">// threeDoubles is of type Double[], and equals [0.0, 0.0, 0.0]</span></code></li>
</ul>
</div>
</section><p class="para">
Thanks to type inference, you don’t need to specify the type to be stored in the array when using this initializer, because it can be inferred from the default value:
</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">anotherThreeDoubles</span> = <span class="vc">Array</span>(<span class="vc">count</span>: <span class="m">3</span>, <span class="vc">repeatedValue</span>: <span class="m">2.5</span>)</code></li>
<li><code class="code-voice"><span class="c">// anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5]</span></code></li>
</ul>
</div>
</section><p class="para">
Finally, you can create a new array by adding together two existing arrays of compatible type with the addition operator (<code class="code-voice">+</code>). The new array’s type is inferred from the type of the two arrays you add together:
</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">sixDoubles</span> = <span class="vc">threeDoubles</span> + <span class="vc">anotherThreeDoubles</span></code></li>
<li><code class="code-voice"><span class="c">// sixDoubles is inferred as Double[], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]</span></code></li>
</ul>
</div>
</section>
</section>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_143"></a>
<h3 class="section-name" tabindex="0">Dictionaries</h3>
<p class="para">
A <em>dictionary</em> is a container that stores multiple values of the same type. Each value is associated with a unique <em>key</em>, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a specified order. You use a dictionary when you need to look up values based on their identifier, in much the same way that a real-world dictionary is used to look up the definition for a particular word.
</p><p class="para">
Swift dictionaries are specific about the types of keys and values they can store. They differ from Objective-C’s <code class="code-voice">NSDictionary</code> and <code class="code-voice">NSMutableDictionary</code> classes, which can use any kind of object as their keys and values and do not provide any information about the nature of these objects. In Swift, the type of keys and values that a particular dictionary can store is always made clear, either through an explicit type annotation or through type inference.
</p><p class="para">
Swift’s dictionary type is written as <code class="code-voice">Dictionary<KeyType, ValueType></code>, where <code class="code-voice">KeyType</code> is the type of value that can be used as a dictionary key, and <code class="code-voice">ValueType</code> is the type of value that the dictionary stores for those keys.
</p><p class="para">
The only restriction is that <code class="code-voice">KeyType</code> must be <em>hashable</em>—that is, it must provide a way to make itself uniquely representable. 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, and all of these types can be used as the keys of a dictionary. Enumeration member values without associated values (as described in <span class="x-name"><a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185">Enumerations</a></span>) are also hashable by default.
</p>
<section class="section">
<a name="TP40014097-CH8-XID_144"></a>
<h3 class="section-name" tabindex="0">Dictionary Literals</h3>
<p class="para">
You can initialize a dictionary with with a <em>dictionary literal</em>, which has a similar syntax to the array literal seen earlier. A dictionary literal is a shorthand way to write one or more key-value pairs as a <code class="code-voice">Dictionary</code> collection.
</p><p class="para">
A <em>key-value pair</em> is a combination of a key and a value. In a dictionary literal, the key and value in each key-value pair are separated by a colon. The key-value pairs are written as a list, separated by commas, surrounded by a pair of square brackets:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice">[<em class="variable-text">key 1</em>: <em class="variable-text">value 1</em>, <em class="variable-text">key 2</em>: <em class="variable-text">value 2</em>, <em class="variable-text">key 3</em>: <em class="variable-text">value 3</em>]</pre></li>
</ul>
</div><p class="para">
The example below creates a dictionary to store the names of international airports. In this dictionary, the keys are three-letter International Air Transport Association codes, and the values are airport names:
</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">airports</span>: <span class="n"></span>> = [<span class="s">"TYO"</span>: <span class="s">"Tokyo"</span>, <span class="s">"DUB"</span>: <span class="s">"Dublin"</span>]</code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">airports</code> dictionary is declared as having a type of <code class="code-voice">Dictionary<String, String></code>, which means “a <code class="code-voice">Dictionary</code> whose keys are of type <code class="code-voice">String</code>, and whose values are also of type <code class="code-voice">String</code>”.
</p><div class="note">
<a name="TP40014097-CH8-XID_145"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">The <code class="code-voice">airports</code> dictionary is declared as a variable (with the <code class="code-voice">var</code> introducer), and not a constant (with the <code class="code-voice">let</code> introducer), because more airports will be added to the dictionary in the examples below.
</p>
</aside>
</div><p class="para">
The <code class="code-voice">airports</code> dictionary is initialized with a dictionary literal containing two key-value pairs. The first pair has a key of <code class="code-voice">"TYO"</code> and a value of <code class="code-voice">"Tokyo"</code>. The second pair has a key of <code class="code-voice">"DUB"</code> and a value of <code class="code-voice">"Dublin"</code>.
</p><p class="para">
This dictionary literal contains two <code class="code-voice">String: String</code> pairs. This matches the type of the <code class="code-voice">airports</code> variable declaration (a dictionary with only <code class="code-voice">String</code> keys, and only <code class="code-voice">String</code> values) and so the assignment of the dictionary literal is permitted as a way to initialize the <code class="code-voice">airports</code> dictionary with two initial items.
</p><p class="para">
As with arrays, you don’t have to write the type of the dictionary if you’re initializing it with a dictionary literal whose keys and values have consistent types. The initialization of <code class="code-voice">airports</code> could have been be written in a shorter form instead:
</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">airports</span> = [<span class="s">"TYO"</span>: <span class="s">"Tokyo"</span>, <span class="s">"DUB"</span>: <span class="s">"Dublin"</span>]</code></li>
</ul>
</div>
</section><p class="para">
Because all keys in the literal are of the same type as each other, and likewise all values are of the same type as each other, Swift can infer that <code class="code-voice">Dictionary<String, String></code> is the correct type to use for the <code class="code-voice">airports</code> dictionary.
</p>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_146"></a>
<h3 class="section-name" tabindex="0">Accessing and Modifying a Dictionary</h3>
<p class="para">
You access and modify a dictionary through its methods and properties, or by using subscript syntax. As with an array, you can find out the number of items in a <code class="code-voice">Dictionary</code> by checking its read-only <code class="code-voice">count</code> property:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"The dictionary of airports contains </span>\(<span class="vc">airports</span>.<span class="vc">count</span>)<span class="s"> items."</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "The dictionary of airports contains 2 items."</span></code></li>
</ul>
</div>
</section><p class="para">
You can add a new item to a dictionary with subscript syntax. Use a new key of the appropriate type as the subscript index, and assign a new value of the appropriate type:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">airports</span>[<span class="s">"LHR"</span>] = <span class="s">"London"</span></code></li>
<li><code class="code-voice"><span class="c">// the airports dictionary now contains 3 items</span></code></li>
</ul>
</div>
</section><p class="para">
You can also use subscript syntax to change the value associated with a particular key:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">airports</span>[<span class="s">"LHR"</span>] = <span class="s">"London Heathrow"</span></code></li>
<li><code class="code-voice"><span class="c">// the value for "LHR" has been changed to "London Heathrow"</span></code></li>
</ul>
</div>
</section><p class="para">
As an alternative to subscripting, use a dictionary’s <code class="code-voice">updateValue(forKey:)</code> method to set or update the value for a particular key. Like the subscript examples above, the <code class="code-voice">updateValue(forKey:)</code> method sets a value for a key if none exists, or updates the value if that key already exists. Unlike a subscript, however, the <code class="code-voice">updateValue(forKey:)</code> method returns the <em>old</em> value after performing an update. This enables you to check whether or not an update took place.
</p><p class="para">
The <code class="code-voice">updateValue(forKey:)</code> method returns an optional value of the dictionary’s value type. For a dictionary that stores <code class="code-voice">String</code> values, for example, the method returns a value of type <code class="code-voice">String?</code>, or “optional <code class="code-voice">String</code>”. This optional value contains the old value for that key if one existed before the update, or <code class="code-voice">nil</code> if no value existed:
</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">if</span> <span class="kt">let</span> <span class="vc">oldValue</span> = <span class="vc">airports</span>.<span class="vc">updateValue</span>(<span class="s">"Dublin International"</span>, <span class="vc">forKey</span>: <span class="s">"DUB"</span>) {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The old value for DUB was </span>\(<span class="vc">oldValue</span>)<span class="s">."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "The old value for DUB was Dublin."</span></code></li>
</ul>
</div>
</section><p class="para">
You can also use subscript syntax to retrieve a value from the dictionary for a particular key. Because it is possible to request a key for which no value exists, a dictionary’s subscript returns an optional value of the dictionary’s value type. If the dictionary contains a value for the requested key, the subscript returns an optional value containing the existing value for that key. Otherwise, the subscript returns <code class="code-voice">nil</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">if</span> <span class="kt">let</span> <span class="vc">airportName</span> = <span class="vc">airports</span>[<span class="s">"DUB"</span>] {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The name of the airport is </span>\(<span class="vc">airportName</span>)<span class="s">."</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">"That airport is not in the airports dictionary."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "The name of the airport is Dublin International."</span></code></li>
</ul>
</div>
</section><p class="para">
You can use subscript syntax to remove a key-value pair from a dictionary by assigning a value of <code class="code-voice">nil</code> for that key:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">airports</span>[<span class="s">"APL"</span>] = <span class="s">"Apple International"</span></code></li>
<li><code class="code-voice"><span class="c">// "Apple International" is not the real airport for APL, so delete it</span></code></li>
<li><code class="code-voice"><span class="vc">airports</span>[<span class="s">"APL"</span>] = <span class="vc">nil</span></code></li>
<li><code class="code-voice"><span class="c">// APL has now been removed from the dictionary</span></code></li>
</ul>
</div>
</section><p class="para">
Alternatively, remove a key-value pair from a dictionary with the <code class="code-voice">removeValueForKey</code> method. This method removes the key-value pair if it exists and returns the removed value, or returns <code class="code-voice">nil</code> if no value existed:
</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">if</span> <span class="kt">let</span> <span class="vc">removedValue</span> = <span class="vc">airports</span>.<span class="vc">removeValueForKey</span>(<span class="s">"DUB"</span>) {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The removed airport's name is </span>\(<span class="vc">removedValue</span>)<span class="s">."</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">"The airports dictionary does not contain a value for DUB."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "The removed airport's name is Dublin International."</span></code></li>
</ul>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_147"></a>
<h3 class="section-name" tabindex="0">Iterating Over a Dictionary</h3>
<p class="para">
You can iterate over the key-value pairs in a dictionary with a <code class="code-voice">for</code>-<code class="code-voice">in</code> loop. Each item in the dictionary is returned as a <code class="code-voice">(key, value)</code> tuple, and you can decompose the tuple’s members into temporary constants or variables as part of the iteration:
</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="vc">airportCode</span>, <span class="vc">airportName</span>) <span class="kt">in</span> <span class="vc">airports</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"</span>\(<span class="vc">airportCode</span>)<span class="s">: </span>\(<span class="vc">airportName</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// TYO: Tokyo</span></code></li>
<li><code class="code-voice"><span class="c">// LHR: London Heathrow</span></code></li>
</ul>
</div>
</section><p class="para">
For more about the <code class="code-voice">for</code>-<code class="code-voice">in</code> loop, see <span class="x-name"><a href="Control Flow.html#TP40014097-CH9-XID_154" data-id="TP40014097-CH9-XID_154">For Loops</a></span>.
</p><p class="para">
You can also retrieve an iteratable collection of a dictionary’s keys or values by accessing its <code class="code-voice">keys</code> and <code class="code-voice">values</code> properties:
</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="vc">airportCode</span> <span class="kt">in</span> <span class="vc">airports</span>.<span class="vc">keys</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"Airport code: </span>\(<span class="vc">airportCode</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// Airport code: TYO</span></code></li>
<li><code class="code-voice"><span class="c">// Airport code: LHR</span></code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="kt">for</span> <span class="vc">airportName</span> <span class="kt">in</span> <span class="vc">airports</span>.<span class="vc">values</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"Airport name: </span>\(<span class="vc">airportName</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// Airport name: Tokyo</span></code></li>
<li><code class="code-voice"><span class="c">// Airport name: London Heathrow</span></code></li>
</ul>
</div>
</section><p class="para">
If you need to use a dictionary’s keys or values with an API that takes an <code class="code-voice">Array</code> instance, initialize a new array with the <code class="code-voice">keys</code> or <code class="code-voice">values</code> property:
</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">airportCodes</span> = <span class="vc">Array</span>(<span class="vc">airports</span>.<span class="vc">keys</span>)</code></li>
<li><code class="code-voice"><span class="c">// airportCodes is ["TYO", "LHR"]</span></code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">airportNames</span> = <span class="vc">Array</span>(<span class="vc">airports</span>.<span class="vc">values</span>)</code></li>
<li><code class="code-voice"><span class="c">// airportNames is ["Tokyo", "London Heathrow"]</span></code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH8-XID_148"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Swift’s <code class="code-voice">Dictionary</code> type is an unordered collection. The order in which keys, values, and key-value pairs are retrieved when iterating over a dictionary is not specified.
</p>
</aside>
</div>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_149"></a>
<h3 class="section-name" tabindex="0">Creating an Empty Dictionary</h3>
<p class="para">
As with arrays, you can create an empty <code class="code-voice">Dictionary</code> of a certain type by using 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">namesOfIntegers</span> = <span class="vc">Dictionary</span><<span class="n"></span>>()</code></li>
<li><code class="code-voice"><span class="c">// namesOfIntegers is an empty Dictionary<Int, String></span></code></li>
</ul>
</div>
</section><p class="para">
This example creates an empty dictionary of type <code class="code-voice">Int</code>, <code class="code-voice">String</code> to store human-readable names of integer values. Its keys are of type <code class="code-voice">Int</code>, and its values are of type <code class="code-voice">String</code>.
</p><p class="para">
If the context already provides type information, create an empty dictionary with an empty dictionary literal, which is written as <code class="code-voice">[:]</code> (a colon inside a pair of square brackets):
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">namesOfIntegers</span>[<span class="m">16</span>] = <span class="s">"sixteen"</span></code></li>
<li><code class="code-voice"><span class="c">// namesOfIntegers now contains 1 key-value pair</span></code></li>
<li><code class="code-voice"><span class="vc">namesOfIntegers</span> = [:]</code></li>
<li><code class="code-voice"><span class="c">// namesOfIntegers is once again an empty dictionary of type Int, String</span></code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH8-XID_150"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Behind the scenes, Swift’s array and dictionary types are implemented as <em>generic collections</em>. For more on generic types and collections, see <span class="x-name"><a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234">Generics</a></span>.
</p>
</aside>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH8-XID_151"></a>
<h3 class="section-name" tabindex="0">Mutability of Collections</h3>
<p class="para">
Arrays and dictionaries store multiple values together in a single collection. If you create an array or a dictionary and assign it to a variable, the collection that is created will be <em>mutable</em>. This means that you can change (or <em>mutate</em>) the size of the collection after it is created by adding more items to the collection, or by removing existing items from the ones it already contains. Conversely, if you assign an array or a dictionary to a constant, that array or dictionary is <em>immutable</em>, and its size cannot be changed.
</p><p class="para">
For dictionaries, immutability also means that you cannot replace the value for an existing key in the dictionary. An immutable dictionary’s contents cannot be changed once they are set.
</p><p class="para">
Immutability has a slightly different meaning for arrays, however. You are still not allowed to perform any action that has the potential to change the size of an immutable array, but you <em>are</em> allowed to set a new value for an existing index in the array. This enables Swift’s <code class="code-voice">Array</code> type to provide optimal performance for array operations when the size of an array is fixed.
</p><p class="para">
The mutability behavior of Swift’s <code class="code-voice">Array</code> type also affects how array instances are assigned and modified. For more information, see <span class="x-name"><a href="Classes and Structures.html#TP40014097-CH13-XID_109" data-id="TP40014097-CH13-XID_109">Assignment and Copy Behavior for Collection Types</a></span>.
</p><div class="note">
<a name="TP40014097-CH8-XID_152"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">It is good practice to create immutable collections in all cases where the collection’s size does not need to change. Doing so enables the Swift compiler to optimize the performance of the collections you create.
</p>
</aside>
</div>
</section>
<section id="next_previous" class="">
<p class="previous-link"><a href="Strings and Characters.html#TP40014097-CH7-XID_368" data-id="TP40014097-CH7-XID_368">Strings and Characters</a></p>
<p class="next-link"><a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153">Control Flow</a></p>
<p class="copyright">Copyright © 2014 Apple Inc. All rights reserved. <a class="link" href="http://www.apple.com/legal/terms/site.html" target="_blank" rel="external">Terms of Use</a> | <a class="link" href="http://www.apple.com/privacy/" target="_blank" rel="external">Privacy Policy</a> | Updated: 2014-06-02
</p>
</section>
</article>
</div>
<div id="big_button"></div>
<footer id="footer">
<div class="content-wrapper">
</div>
<script src="./resource/devpubs-1.1.15.js" type="text/javascript" charset="utf-8"></script> </footer> <div id="blackout"> <div id="preload"></div> </div> </body></html>