-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasses and Structures.html
More file actions
1012 lines (830 loc) · 65.8 KB
/
Copy pathClasses and Structures.html
File metadata and controls
1012 lines (830 loc) · 65.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: Classes and Structures</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 ">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133">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 nav-current-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94" class="nav-chapter-active">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-CH13"></a><a name="TP40014097-CH13-XID_94"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Classes and Structures</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-CH13-XID_96">
Comparing Classes and Structures
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH13-XID_104">
Structures and Enumerations Are Value Types
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH13-XID_105">
Classes Are Reference Types
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH13-XID_108">
Choosing Between Classes and Structures
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH13-XID_109">
Assignment and Copy Behavior for Collection Types
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
<em>Classes</em> and <em>structures</em> are general-purpose, flexible constructs that become the building blocks of your program’s code. You define properties and methods to add functionality to your classes and structures by using exactly the same syntax as for constants, variables, and functions.
</p>
<p class="para">
Unlike other programming languages, Swift does not require you to create separate interface and implementation files for custom classes and structures. In Swift, you define a class or a structure in a single file, and the external interface to that class or structure is automatically made available for other code to use.
</p>
<div class="note">
<a name="TP40014097-CH13-XID_95"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">An instance of a <em>class</em> is traditionally known as an <em>object</em>. However, Swift classes and structures are much closer in functionality than in other languages, and much of this chapter describes functionality that can apply to instances of <em>either</em> a class or a structure type. Because of this, the more general term <em>instance</em> is used.
</p>
</aside>
</div>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_96"></a>
<h3 class="section-name" tabindex="0">Comparing Classes and Structures</h3>
<p class="para">
Classes and structures in Swift have many things in common. Both can:
</p><ul class="list-bullet">
<li class="item"><p class="para">
Define properties to store values
</p>
</li><li class="item"><p class="para">
Define methods to provide functionality
</p>
</li><li class="item"><p class="para">
Define subscripts to provide access to their values using subscript syntax
</p>
</li><li class="item"><p class="para">
Define initializers to set up their initial state
</p>
</li><li class="item"><p class="para">
Be extended to expand their functionality beyond a default implementation
</p>
</li><li class="item"><p class="para">
Conform to protocols to provide standard functionality of a certain kind
</p>
</li>
</ul><p class="para">
For more information, see <span class="x-name"><a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323">Properties</a></span>, <span class="x-name"><a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300">Methods</a></span>, <span class="x-name"><a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393">Subscripts</a></span>, <span class="x-name"><a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a></span>, <span class="x-name"><a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191">Extensions</a></span>, and <span class="x-name"><a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345">Protocols</a></span>.
</p><p class="para">
Classes have additional capabilities that structures do not:
</p><ul class="list-bullet">
<li class="item"><p class="para">
Inheritance enables one class to inherit the characteristics of another.
</p>
</li><li class="item"><p class="para">
Type casting enables you to check and interpret the type of a class instance at runtime.
</p>
</li><li class="item"><p class="para">
Deinitializers enable an instance of a class to free up any resources it has assigned.
</p>
</li><li class="item"><p class="para">
Reference counting allows more than one reference to a class instance.
</p>
</li>
</ul><p class="para">
For more information, see <span class="x-name"><a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251">Inheritance</a></span>, <span class="x-name"><a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443">Type Casting</a></span>, <span class="x-name"><a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a></span>, and <span class="x-name"><a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50">Automatic Reference Counting</a></span>.
</p><div class="note">
<a name="TP40014097-CH13-XID_97"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Structures are always copied when they are passed around in your code, and do not use reference counting.
</p>
</aside>
</div>
<section class="section">
<a name="TP40014097-CH13-XID_98"></a>
<h3 class="section-name" tabindex="0">Definition Syntax</h3>
<p class="para">
Classes and structures have a similar definition syntax. You introduce classes with the <code class="code-voice">class</code> keyword and structures with the <code class="code-voice">struct</code> keyword. Both place their entire definition within a pair of braces:
</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">class</span> <span class="vc">SomeClass</span> {</code></li>
<li><code class="code-voice"> <span class="c">// class definition goes here</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="kt">struct</span> <span class="vc">SomeStructure</span> {</code></li>
<li><code class="code-voice"> <span class="c">// structure definition goes here</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH13-XID_99"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Whenever you define a new class or structure, you effectively define a brand new Swift type. Give types <code class="code-voice">UpperCamelCase</code> names (such as <code class="code-voice">SomeClass</code> and <code class="code-voice">SomeStructure</code> here) to match the capitalization of standard Swift types (such as <code class="code-voice">String</code>, <code class="code-voice">Int</code>, and <code class="code-voice">Bool</code>). Conversely, always give properties and methods <code class="code-voice">lowerCamelCase</code> names (such as <code class="code-voice">frameRate</code> and <code class="code-voice">incrementCount</code>) to differentiate them from type names.
</p>
</aside>
</div><p class="para">
Here’s an example of a structure definition and a class definition:
</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">Resolution</span> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">width</span> = <span class="m">0</span></code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">height</span> = <span class="m">0</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">VideoMode</span> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">resolution</span> = <span class="vc">Resolution</span>()</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">interlaced</span> = <span class="vc">false</span></code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">frameRate</span> = <span class="m">0.0</span></code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">name</span>: <span class="n"></span>?</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The example above defines a new structure called <code class="code-voice">Resolution</code>, to describe a pixel-based display resolution. This structure has two stored properties called <code class="code-voice">width</code> and <code class="code-voice">height</code>. Stored properties are constants or variables that are bundled up and stored as part of the class or structure. These two properties are inferred to be of type <code class="code-voice">Int</code> by setting them to an initial integer value of <code class="code-voice">0</code>.
</p><p class="para">
The example above also defines a new class called <code class="code-voice">VideoMode</code>, to describe a specific video mode for video display. This class has four variable stored properties. The first, <code class="code-voice">resolution</code>, is initialized with a new <code class="code-voice">Resolution</code> structure instance, which infers a property type of <code class="code-voice">Resolution</code>. For the other three properties, new <code class="code-voice">VideoMode</code> instances will be initialized with an <code class="code-voice">interlaced</code> setting of <code class="code-voice">false</code> (meaning “non-interlaced video”), a playback frame rate of <code class="code-voice">0.0</code>, and an optional <code class="code-voice">String</code> value called <code class="code-voice">name</code>. The <code class="code-voice">name</code> property is automatically given a default value of <code class="code-voice">nil</code>, or “no <code class="code-voice">name</code> value”, because it is of an optional type.
</p>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_100"></a>
<h3 class="section-name" tabindex="0">Class and Structure Instances</h3>
<p class="para">
The <code class="code-voice">Resolution</code> structure definition and the <code class="code-voice">VideoMode</code> class definition only describe what a <code class="code-voice">Resolution</code> or <code class="code-voice">VideoMode</code> will look like. They themselves do not describe a specific resolution or video mode. To do that, you need to create an instance of the structure or class.
</p><p class="para">
The syntax for creating instances is very similar for both structures and classes:
</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">someResolution</span> = <span class="vc">Resolution</span>()</code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">someVideoMode</span> = <span class="vc">VideoMode</span>()</code></li>
</ul>
</div>
</section><p class="para">
Structures and classes both use initializer syntax for new instances. The simplest form of initializer syntax uses the type name of the class or structure followed by empty parentheses, such as <code class="code-voice">Resolution()</code> or <code class="code-voice">VideoMode()</code>. This creates a new instance of the class or structure, with any properties initialized to their default values. Class and structure initialization is described in more detail in <span class="x-name"><a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a></span>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_101"></a>
<h3 class="section-name" tabindex="0">Accessing Properties</h3>
<p class="para">
You can access the properties of an instance using <em>dot syntax</em>. In dot syntax, you write the property name immediately after the instance name, separated by a period (<code class="code-voice">.</code>), without any spaces:
</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 width of someResolution is </span>\(<span class="vc">someResolution</span>.<span class="vc">width</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "The width of someResolution is 0"</span></code></li>
</ul>
</div>
</section><p class="para">
In this example, <code class="code-voice">someResolution.width</code> refers to the <code class="code-voice">width</code> property of <code class="code-voice">someResolution</code>, and returns its default initial value of <code class="code-voice">0</code>.
</p><p class="para">
You can drill down into sub-properties, such as the <code class="code-voice">width</code> property in the <code class="code-voice">resolution</code> property of a <code class="code-voice">VideoMode</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">println</span>(<span class="s">"The width of someVideoMode is </span>\(<span class="vc">someVideoMode</span>.<span class="vc">resolution</span>.<span class="vc">width</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "The width of someVideoMode is 0"</span></code></li>
</ul>
</div>
</section><p class="para">
You can also use dot syntax to assign a new value to a variable 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">someVideoMode</span>.<span class="vc">resolution</span>.<span class="vc">width</span> = <span class="m">1280</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"The width of someVideoMode is now </span>\(<span class="vc">someVideoMode</span>.<span class="vc">resolution</span>.<span class="vc">width</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "The width of someVideoMode is now 1280"</span></code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH13-XID_102"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Unlike Objective-C, Swift enables you to set sub-properties of a structure property directly. In the last example above, the <code class="code-voice">width</code> property of the <code class="code-voice">resolution</code> property of <code class="code-voice">someVideoMode</code> is set directly, without your needing to set the entire <code class="code-voice">resolution</code> property to a new value.
</p>
</aside>
</div>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_103"></a>
<h3 class="section-name" tabindex="0">Memberwise Initializers for Structure Types</h3>
<p class="para">
All structures have an automatically-generated <em>memberwise initializer</em>, which you can use to initialize the member properties of new structure instances. Initial values for the properties of the new instance can be passed to the memberwise initializer by name:
</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">vga</span> = <span class="vc">Resolution</span>(<span class="vc">width</span>: <span class="m">640</span>, <span class="vc">height</span>: <span class="m">480</span>)</code></li>
</ul>
</div>
</section><p class="para">
Unlike structures, class instances do not receive a default memberwise initializer. Initializers are described in more detail in <span class="x-name"><a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a></span>.
</p>
</section>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_104"></a>
<h3 class="section-name" tabindex="0">Structures and Enumerations Are Value Types</h3>
<p class="para">
A <em>value type</em> is a type that is <em>copied</em> when it is assigned to a variable or constant, or when it is passed to a function.
</p><p class="para">
You’ve actually been using value types extensively throughout the previous chapters. In fact, all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types, and are implemented as structures behind the scenes.
</p><p class="para">
All structures and enumerations are value types in Swift. This means that any structure and enumeration instances you create—and any value types they have as properties—are always copied when they are passed around in your code.
</p><p class="para">
Consider this example, which uses the <code class="code-voice">Resolution</code> structure from the previous 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">let</span> <span class="vc">hd</span> = <span class="vc">Resolution</span>(<span class="vc">width</span>: <span class="m">1920</span>, <span class="vc">height</span>: <span class="m">1080</span>)</code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">cinema</span> = <span class="vc">hd</span></code></li>
</ul>
</div>
</section><p class="para">
This example declares a constant called <code class="code-voice">hd</code> and sets it to a <code class="code-voice">Resolution</code> instance initialized with the width and height of full HD video (<code class="code-voice">1920</code> pixels wide by <code class="code-voice">1080</code> pixels high).
</p><p class="para">
It then declares a variable called <code class="code-voice">cinema</code> and sets it to the current value of <code class="code-voice">hd</code>. Because <code class="code-voice">Resolution</code> is a structure, a <em>copy</em> of the existing instance is made, and this new copy is assigned to <code class="code-voice">cinema</code>. Even though <code class="code-voice">hd</code> and <code class="code-voice">cinema</code> now have the same width and height, they are two completely different instances behind the scenes.
</p><p class="para">
Next, the <code class="code-voice">width</code> property of <code class="code-voice">cinema</code> is amended to be the width of the slightly-wider 2K standard used for digital cinema projection (<code class="code-voice">2048</code> pixels wide and <code class="code-voice">1080</code> pixels high):
</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">cinema</span>.<span class="vc">width</span> = <span class="m">2048</span></code></li>
</ul>
</div>
</section><p class="para">
Checking the <code class="code-voice">width</code> property of <code class="code-voice">cinema</code> shows that it has indeed changed to be <code class="code-voice">2048</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">println</span>(<span class="s">"cinema is now </span>\(<span class="vc">cinema</span>.<span class="vc">width</span>)<span class="s"> pixels wide"</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "cinema is now 2048 pixels wide"</span></code></li>
</ul>
</div>
</section><p class="para">
However, the <code class="code-voice">width</code> property of the original <code class="code-voice">hd</code> instance still has the old value of <code class="code-voice">1920</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">println</span>(<span class="s">"hd is still </span>\(<span class="vc">hd</span>.<span class="vc">width</span>)<span class="s"> pixels wide"</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "hd is still 1920 pixels wide"</span></code></li>
</ul>
</div>
</section><p class="para">
When <code class="code-voice">cinema</code> was given the current value of <code class="code-voice">hd</code>, the <em>values</em> stored in <code class="code-voice">hd</code> were copied into the new <code class="code-voice">cinema</code> instance. The end result is two completely separate instances, which just happened to contain the same numeric values. Because they are separate instances, setting the width of <code class="code-voice">cinema</code> to <code class="code-voice">2048</code> doesn’t affect the width stored in <code class="code-voice">hd</code>.
</p><p class="para">
The same behavior applies to enumerations:
</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">enum</span> <span class="vc">CompassPoint</span> {</code></li>
<li><code class="code-voice"> <span class="kt">case</span> <span class="vc">North</span>, <span class="vc">South</span>, <span class="vc">East</span>, <span class="vc">West</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">currentDirection</span> = <span class="vc">CompassPoint</span>.<span class="vc">West</span></code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">rememberedDirection</span> = <span class="vc">currentDirection</span></code></li>
<li><code class="code-voice"><span class="vc">currentDirection</span> = .<span class="vc">East</span></code></li>
<li><code class="code-voice"><span class="kt">if</span> <span class="vc">rememberedDirection</span> == .<span class="vc">West</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"The remembered direction is still .West"</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "The remembered direction is still .West"</span></code></li>
</ul>
</div>
</section><p class="para">
When <code class="code-voice">rememberedDirection</code> is assigned the value of <code class="code-voice">currentDirection</code>, it is actually set to a copy of that value. Changing the value of <code class="code-voice">currentDirection</code> thereafter does not affect the copy of the original value that was stored in <code class="code-voice">rememberedDirection</code>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_105"></a>
<h3 class="section-name" tabindex="0">Classes Are Reference Types</h3>
<p class="para">
Unlike value types, <em>reference types</em> are <em>not</em> copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.
</p><p class="para">
Here’s an example, using the <code class="code-voice">VideoMode</code> class defined above:
</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">tenEighty</span> = <span class="vc">VideoMode</span>()</code></li>
<li><code class="code-voice"><span class="vc">tenEighty</span>.<span class="vc">resolution</span> = <span class="vc">hd</span></code></li>
<li><code class="code-voice"><span class="vc">tenEighty</span>.<span class="vc">interlaced</span> = <span class="vc">true</span></code></li>
<li><code class="code-voice"><span class="vc">tenEighty</span>.<span class="vc">name</span> = <span class="s">"1080i"</span></code></li>
<li><code class="code-voice"><span class="vc">tenEighty</span>.<span class="vc">frameRate</span> = <span class="m">25.0</span></code></li>
</ul>
</div>
</section><p class="para">
This example declares a new constant called <code class="code-voice">tenEighty</code> and sets it to refer to a new instance of the <code class="code-voice">VideoMode</code> class. The video mode is assigned a copy of the HD resolution of <code class="code-voice">1920</code> by <code class="code-voice">1080</code> from before. It is set to be interlaced, and is given a name of <code class="code-voice">"1080i"</code>. Finally, it is set to a frame rate of <code class="code-voice">25.0</code> frames per second.
</p><p class="para">
Next, <code class="code-voice">tenEighty</code> is assigned to a new constant, called <code class="code-voice">alsoTenEighty</code>, and the frame rate of <code class="code-voice">alsoTenEighty</code> is modified:
</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">alsoTenEighty</span> = <span class="vc">tenEighty</span></code></li>
<li><code class="code-voice"><span class="vc">alsoTenEighty</span>.<span class="vc">frameRate</span> = <span class="m">30.0</span></code></li>
</ul>
</div>
</section><p class="para">
Because classes are reference types, <code class="code-voice">tenEighty</code> and <code class="code-voice">alsoTenEighty</code> actually both refer to the <em>same</em> <code class="code-voice">VideoMode</code> instance. Effectively, they are just two different names for the same single instance.
</p><p class="para">
Checking the <code class="code-voice">frameRate</code> property of <code class="code-voice">tenEighty</code> shows that it correctly reports the new frame rate of <code class="code-voice">30.0</code> from the underlying <code class="code-voice">VideoMode</code> instance:
</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 frameRate property of tenEighty is now </span>\(<span class="vc">tenEighty</span>.<span class="vc">frameRate</span>)<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "The frameRate property of tenEighty is now 30.0"</span></code></li>
</ul>
</div>
</section><p class="para">
Note that <code class="code-voice">tenEighty</code> and <code class="code-voice">alsoTenEighty</code> are declared as <em>constants</em>, rather than variables. However, you can still change <code class="code-voice">tenEighty.frameRate</code> and <code class="code-voice">alsoTenEighty.frameRate</code> because the values of the <code class="code-voice">tenEighty</code> and <code class="code-voice">alsoTenEighty</code> constants themselves do not actually change. <code class="code-voice">tenEighty</code> and <code class="code-voice">alsoTenEighty</code> themselves do not “store” the <code class="code-voice">VideoMode</code> instance—instead, they both <em>refer</em> to a <code class="code-voice">VideoMode</code> instance behind the scenes. It is the <code class="code-voice">frameRate</code> property of the underlying <code class="code-voice">VideoMode</code> that is changed, not the values of the constant references to that <code class="code-voice">VideoMode</code>.
</p>
<section class="section">
<a name="TP40014097-CH13-XID_106"></a>
<h3 class="section-name" tabindex="0">Identity Operators</h3>
<p class="para">
Because classes are reference types, it is possible for multiple constants and variables to refer to the same single instance of a class behind the scenes. (The same is not true for structures and enumerations, because they are value types and are always copied when they are assigned to a constant or variable, or passed to a function.)
</p><p class="para">
It can sometimes be useful to find out if two constants or variables refer to exactly the same instance of a class. To enable this, Swift provides two identity operators:
</p><ul class="list-bullet">
<li class="item"><p class="para">
Identical to (<code class="code-voice">===</code>)
</p>
</li><li class="item"><p class="para">
Not identical to (<code class="code-voice">!==</code>)
</p>
</li>
</ul><p class="para">
Use these operators to check whether two constants or variables refer to the same single instance:
</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">tenEighty</span> === <span class="vc">alsoTenEighty</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"tenEighty and alsoTenEighty refer to the same Resolution instance."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "tenEighty and alsoTenEighty refer to the same Resolution instance."</span></code></li>
</ul>
</div>
</section><p class="para">
Note that “identical to” (represented by three equals signs, or <code class="code-voice">===</code>) does not mean the same thing as “equal to” (represented by two equals signs, or <code class="code-voice">==</code>):
</p><ul class="list-bullet">
<li class="item"><p class="para">
“Identical to” means that two constants or variables of class type refer to exactly the same class instance.
</p>
</li><li class="item"><p class="para">
“Equal to” means that two instances are considered “equal” or “equivalent” in value, for some appropriate meaning of “equal”, as defined by the type’s designer.
</p>
</li>
</ul><p class="para">
When you define your own custom classes and structures, it is your responsibility to decide what qualifies as two instances being “equal”. The process of defining your own implementations of the “equal to” and “not equal to” operators is described in <span class="x-name"><a href="Advanced Operators.html#TP40014097-CH27-XID_47" data-id="TP40014097-CH27-XID_47">Equivalence Operators</a></span>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_107"></a>
<h3 class="section-name" tabindex="0">Pointers</h3>
<p class="para">
If you have experience with C, C++, or Objective-C, you may know that these languages use <em>pointers</em> to refer to addresses in memory. A Swift constant or variable that refers to an instance of some reference type is similar to a pointer in C, but is not a direct pointer to an address in memory, and does not require you to write an asterisk (<code class="code-voice">*</code>) to indicate that you are creating a reference. Instead, these references are defined like any other constant or variable in Swift.
</p>
</section>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_108"></a>
<h3 class="section-name" tabindex="0">Choosing Between Classes and Structures</h3>
<p class="para">
You can use both classes and structures to define custom data types to use as the building blocks of your program’s code.
</p><p class="para">
However, structure instances are always passed by <em>value</em>, and class instances are always passed by <em>reference</em>. This means that they are suited to different kinds of tasks. As you consider the data constructs and functionality that you need for a project, decide whether each data construct should be defined as a class or as a structure.
</p><p class="para">
As a general guideline, consider creating a structure when one or more of these conditions apply:
</p><ul class="list-bullet">
<li class="item"><p class="para">
The structure’s primary purpose is to encapsulate a few relatively simple data values.
</p>
</li><li class="item"><p class="para">
It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an instance of that structure.
</p>
</li><li class="item"><p class="para">
Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.
</p>
</li><li class="item"><p class="para">
The structure does not need to inherit properties or behavior from another existing type.
</p>
</li>
</ul><p class="para">
Examples of good candidates for structures include:
</p><ul class="list-bullet">
<li class="item"><p class="para">
The size of a geometric shape, perhaps encapsulating a <code class="code-voice">width</code> property and a <code class="code-voice">height</code> property, both of type <code class="code-voice">Double</code>.
</p>
</li><li class="item"><p class="para">
A way to refer to ranges within a series, perhaps encapsulating a <code class="code-voice">start</code> property and a <code class="code-voice">length</code> property, both of type <code class="code-voice">Int</code>.
</p>
</li><li class="item"><p class="para">
A point in a 3D coordinate system, perhaps encapsulating <code class="code-voice">x</code>, <code class="code-voice">y</code> and <code class="code-voice">z</code> properties, each of type <code class="code-voice">Double</code>.
</p>
</li>
</ul><p class="para">
In all other cases, define a class, and create instances of that class to be managed and passed by reference. In practice, this means that most custom data constructs should be classes, not structures.
</p>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_109"></a>
<h3 class="section-name" tabindex="0">Assignment and Copy Behavior for Collection Types</h3>
<p class="para">
Swift’s <code class="code-voice">Array</code> and <code class="code-voice">Dictionary</code> types are implemented as structures. However, arrays have slightly different copying behavior from dictionaries and other structures when they are assigned to a constant or variable, and when they are passed to a function or method.
</p><p class="para">
The behavior described for <code class="code-voice">Array</code> and <code class="code-voice">Dictionary</code> below is different again from the behavior of <code class="code-voice">NSArray</code> and <code class="code-voice">NSDictionary</code> in Foundation, which are implemented as classes, not structures. <code class="code-voice">NSArray</code> and <code class="code-voice">NSDictionary</code> instances are always assigned and passed around as a reference to an existing instance, rather than as a copy.
</p><div class="note">
<a name="TP40014097-CH13-XID_110"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">The descriptions below refer to the “copying” of arrays, dictionaries, strings, and other values. Where copying is mentioned, the behavior you see in your code will always be as if a copy took place. However, Swift only performs an <em>actual</em> copy behind the scenes when it is absolutely necessary to do so. Swift manages all value copying to ensure optimal performance, and you should not avoid assignment to try to preempt this optimization.
</p>
</aside>
</div>
<section class="section">
<a name="TP40014097-CH13-XID_111"></a>
<h3 class="section-name" tabindex="0">Assignment and Copy Behavior for Dictionaries</h3>
<p class="para">
Whenever you assign a <code class="code-voice">Dictionary</code> instance to a constant or variable, or pass a <code class="code-voice">Dictionary</code> instance as an argument to a function or method call, the dictionary is <em>copied</em> at the point that the assignment or call takes place. This process is described in <span class="x-name"><a href="Classes and Structures.html#TP40014097-CH13-XID_104" data-id="TP40014097-CH13-XID_104">Structures and Enumerations Are Value Types</a></span>.
</p><p class="para">
If the keys and/or values stored in the <code class="code-voice">Dictionary</code> instance are value types (structures or enumerations), they too are copied when the assignment or call takes place. Conversely, if the keys and/or values are reference types (classes or functions), the references are copied, but not the class instances or functions that they refer to. This copy behavior for a dictionary’s keys and values is the same as the copy behavior for a structure’s stored properties when the structure is copied.
</p><p class="para">
The example below defines a dictionary called <code class="code-voice">ages</code>, which stores the names and ages of four people. The <code class="code-voice">ages</code> dictionary is then assigned to a new variable called <code class="code-voice">copiedAges</code> and is copied when this assignment takes place. After the assignment, <code class="code-voice">ages</code> and <code class="code-voice">copiedAges</code> are two separate dictionaries.
</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">ages</span> = [<span class="s">"Peter"</span>: <span class="m">23</span>, <span class="s">"Wei"</span>: <span class="m">35</span>, <span class="s">"Anish"</span>: <span class="m">65</span>, <span class="s">"Katya"</span>: <span class="m">19</span>]</code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">copiedAges</span> = <span class="vc">ages</span></code></li>
</ul>
</div>
</section><p class="para">
The keys for this dictionary are of type <code class="code-voice">String</code>, and the values are of type <code class="code-voice">Int</code>. Both types are value types in Swift, and so the keys and values are also copied when the dictionary copy takes place.
</p><p class="para">
You can prove that the <code class="code-voice">ages</code> dictionary has been copied by changing an age value in one of the dictionaries and checking the corresponding value in the other. If you set the value for <code class="code-voice">"Peter"</code> in the <code class="code-voice">copiedAges</code> dictionary to <code class="code-voice">24</code>, the <code class="code-voice">ages</code> dictionary still returns the old value of <code class="code-voice">23</code> from before the copy took place:
</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">copiedAges</span>[<span class="s">"Peter"</span>] = <span class="m">24</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">ages</span>[<span class="s">"Peter"</span>])</code></li>
<li><code class="code-voice"><span class="c">// prints "23"</span></code></li>
</ul>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_112"></a>
<h3 class="section-name" tabindex="0">Assignment and Copy Behavior for Arrays</h3>
<p class="para">
The assignment and copy behavior for Swift’s <code class="code-voice">Array</code> type is more complex than for its <code class="code-voice">Dictionary</code> type. <code class="code-voice">Array</code> provides C-like performance when you work with an array’s contents and copies an array’s contents only when copying is necessary.
</p><p class="para">
If you assign an <code class="code-voice">Array</code> instance to a constant or variable, or pass an <code class="code-voice">Array</code> instance as an argument to a function or method call, the contents of the array are <em>not</em> copied at the point that the assignment or call takes place. Instead, both arrays share the same sequence of element values. When you modify an element value through one array, the result is observable through the other.
</p><p class="para">
For arrays, copying only takes place when you perform an action that has the potential to modify the <em>length</em> of the array. This includes appending, inserting, or removing items, or using a ranged subscript to replace a range of items in the array. If and when array copying does take place, the copy behavior for an array’s contents is the same as for a dictionary’s keys and values, as described in <span class="x-name"><a href="Classes and Structures.html#TP40014097-CH13-XID_111" data-id="TP40014097-CH13-XID_111">Assignment and Copy Behavior for Dictionaries</a></span>.
</p><p class="para">
The example below assigns a new array of <code class="code-voice">Int</code> values to a variable called <code class="code-voice">a</code>. This array is also assigned to two further variables called <code class="code-voice">b</code> and <code class="code-voice">c</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">a</span> = [<span class="m">1</span>, <span class="m">2</span>, <span class="m">3</span>]</code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">b</span> = <span class="vc">a</span></code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">c</span> = <span class="vc">a</span></code></li>
</ul>
</div>
</section><p class="para">
You can retrieve the first value in the array with subscript syntax on either <code class="code-voice">a</code>, <code class="code-voice">b</code>, or <code class="code-voice">c</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">println</span>(<span class="vc">a</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 1</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">b</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 1</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">c</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 1</span></code></li>
</ul>
</div>
</section><p class="para">
If you set an item in the array to a new value with subscript syntax, all three of <code class="code-voice">a</code>, <code class="code-voice">b</code>, and <code class="code-voice">c</code> will return the new value. Note that the array is not copied when you set a new value with subscript syntax, because setting a single value with subscript syntax does not have the potential to change the array’s length:
</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">a</span>[<span class="m">0</span>] = <span class="m">42</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">a</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 42</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">b</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 42</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">c</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 42</span></code></li>
</ul>
</div>
</section><p class="para">
However, if you append a new item to <code class="code-voice">a</code>, you <em>do</em> modify the array’s length. This prompts Swift to create a new copy of the array at the point that you append the new value. Henceforth, <code class="code-voice">a</code> is a separate, independent copy of the array.
</p><p class="para">
If you change a value in <code class="code-voice">a</code> after the copy is made, <code class="code-voice">a</code> will return a different value from <code class="code-voice">b</code> and <code class="code-voice">c</code>, which both still reference the original array contents from before the copy took place:
</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">a</span>.<span class="vc">append</span>(<span class="m">4</span>)</code></li>
<li><code class="code-voice"><span class="vc">a</span>[<span class="m">0</span>] = <span class="m">777</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">a</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 777</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">b</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 42</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">c</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 42</span></code></li>
</ul>
</div>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_113"></a>
<h3 class="section-name" tabindex="0">Ensuring That an Array Is Unique</h3>
<p class="para">
It can be useful to ensure that you have a unique copy of an array before performing an action on that array’s contents, or before passing that array to a function or method. You ensure the uniqueness of an array reference by calling the <code class="code-voice">unshare</code> method on a variable of array type. (The <code class="code-voice">unshare</code> method cannot be called on a constant array.)
</p><p class="para">
If multiple variables currently refer to the same array, and you call the <code class="code-voice">unshare</code> method on one of those variables, the array is copied, so that the variable has its own independent copy of the array. However, no copying takes place if the variable is already the only reference to the array.
</p><p class="para">
At the end of the previous example, <code class="code-voice">b</code> and <code class="code-voice">c</code> both reference the same array. Call the <code class="code-voice">unshare</code> method on <code class="code-voice">b</code> to make it become a unique copy:
</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">b</span>.<span class="vc">unshare</span>()</code></li>
</ul>
</div>
</section><p class="para">
If you change the first value in <code class="code-voice">b</code> after calling the <code class="code-voice">unshare</code> method, all three arrays will now report a different 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="vc">b</span>[<span class="m">0</span>] = -<span class="m">105</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">a</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 777</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">b</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// -105</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">c</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// 42</span></code></li>
</ul>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_114"></a>
<h3 class="section-name" tabindex="0">Checking Whether Two Arrays Share the Same Elements</h3>
<p class="para">
Check whether two arrays or subarrays share the same storage and elements by comparing them with the identity operators (<code class="code-voice">===</code> and <code class="code-voice">!==</code>).
</p><p class="para">
The example below uses the “identical to” operator (<code class="code-voice">===</code>) to check whether <code class="code-voice">b</code> and <code class="code-voice">c</code> still share the same array elements:
</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">b</span> === <span class="vc">c</span> {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"b and c still share the same array elements."</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">"b and c now refer to two independent sets of array elements."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "b and c now refer to two independent sets of array elements."</span></code></li>
</ul>
</div>
</section><p class="para">
Alternatively, use the identity operators to check whether two subarrays share the same elements. The example below compares two identical subarrays from <code class="code-voice">b</code> and confirms that they refer to the same elements:
</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">b</span>[<span class="m">0</span>...<span class="m">1</span>] === <span class="vc">b</span>[<span class="m">0</span>...<span class="m">1</span>] {</code></li>
<li><code class="code-voice"> <span class="vc">println</span>(<span class="s">"These two subarrays share the same elements."</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">"These two subarrays do not share the same elements."</span>)</code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// prints "These two subarrays share the same elements."</span></code></li>
</ul>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH13-XID_115"></a>
<h3 class="section-name" tabindex="0">Forcing a Copy of an Array</h3>
<p class="para">
Force an explicit copy of an array by calling the array’s <code class="code-voice">copy</code> method. This method performs a shallow copy of the array and returns a new array containing the copied items.
</p><p class="para">
The example below defines an array called <code class="code-voice">names</code>, which stores the names of seven people. A new variable called <code class="code-voice">copiedNames</code> is set to the result of calling the <code class="code-voice">copy</code> method on the <code class="code-voice">names</code> 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">names</span> = [<span class="s">"Mohsen"</span>, <span class="s">"Hilary"</span>, <span class="s">"Justyn"</span>, <span class="s">"Amy"</span>, <span class="s">"Rich"</span>, <span class="s">"Graham"</span>, <span class="s">"Vic"</span>]</code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">copiedNames</span> = <span class="vc">names</span>.<span class="vc">copy</span>()</code></li>
</ul>
</div>
</section><p class="para">
You can prove that the <code class="code-voice">names</code> array has been copied by changing an item in one of the arrays and checking the corresponding item in the other. If you set the first item in the <code class="code-voice">copiedNames</code> array to <code class="code-voice">"Mo"</code> rather than <code class="code-voice">"Mohsen"</code>, the <code class="code-voice">names</code> array still returns the old value of <code class="code-voice">"Mohsen"</code> from before the copy took place:
</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">copiedNames</span>[<span class="m">0</span>] = <span class="s">"Mo"</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="vc">names</span>[<span class="m">0</span>])</code></li>
<li><code class="code-voice"><span class="c">// prints "Mohsen"</span></code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH13-XID_116"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">If you simply need to be sure that your reference to an array’s contents is the only reference in existence, call the <code class="code-voice">unshare</code> method, not the <code class="code-voice">copy</code> method. The <code class="code-voice">unshare</code> method does not make a copy of the array unless it is necessary to do so. The <code class="code-voice">copy</code> method always copies the array, even if it is already unshared.
</p>
</aside>
</div>
</section>
</section>
</section>
<section id="next_previous" class="">
<p class="previous-link"><a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185">Enumerations</a></p>
<p class="next-link"><a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323">Properties</a></p>