-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeclarations.html
More file actions
2327 lines (2245 loc) · 167 KB
/
Copy pathDeclarations.html
File metadata and controls
2327 lines (2245 loc) · 167 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: Declarations</title>
<link rel="stylesheet" type="text/css" href="resource/style-1.1.15.css">
<meta charset="utf-8"> <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
<body id="conceptual_flow_with_tasks" class="jazz">
<div id="_omniture_top">
</div>
<a name="TP40014097" title="The Swift Programming Language"></a>
<section id="valence">
<div class="content-wrapper">
<p id="hierarchial_navigation">
<span id="book_title">The Swift Programming Language</span>
</p>
<img id="shortstack" src="./resource/shortstack_2x.png">
</div>
</section>
<div class="content-wrapper">
<nav class="book-parts hideInXcode" role="navigation">
<ul class="nav-parts">
<li data-id="TP40014097-CH1-XID_27" class="part-name">Welcome to Swift
<ul class="nav-chapters">
<li class="nav-chapter nav-visited-chapter">
<a href="About Swift.html#TP40014097-CH3-XID_0" data-id="TP40014097-CH3-XID_0" class="">About Swift</a>
</li>
<li class="nav-chapter">
<a href="A Swift Tour.html#TP40014097-CH2-XID_1" data-id="TP40014097-CH2-XID_1" class="">A Swift Tour</a>
</li>
</ul>
</li><li data-id="TP40014097-CH4-XID_299" class="part-name">Language Guide
<ul class="nav-chapters">
<li class="nav-chapter">
<a href="The Basics.html#TP40014097-CH5-XID_399" data-id="TP40014097-CH5-XID_399" class="">The Basics</a>
</li>
<li class="nav-chapter">
<a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70" class="">Basic Operators</a>
</li>
<li class="nav-chapter">
<a href="Strings and Characters.html#TP40014097-CH7-XID_368" data-id="TP40014097-CH7-XID_368" class="">Strings and Characters</a>
</li>
<li class="nav-chapter">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133" class="">Collection Types</a>
</li>
<li class="nav-chapter">
<a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153" class="">Control Flow</a>
</li>
<li class="nav-chapter">
<a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204" class="">Functions</a>
</li>
<li class="nav-chapter">
<a href="Closures.html#TP40014097-CH11-XID_117" data-id="TP40014097-CH11-XID_117" class="">Closures</a>
</li>
<li class="nav-chapter">
<a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185" class="">Enumerations</a>
</li>
<li class="nav-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94" class="">Classes and Structures</a>
</li>
<li class="nav-chapter">
<a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323" class="">Properties</a>
</li>
<li class="nav-chapter">
<a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300" class="">Methods</a>
</li>
<li class="nav-chapter">
<a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393" class="">Subscripts</a>
</li>
<li class="nav-chapter">
<a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251" class="">Inheritance</a>
</li>
<li class="nav-chapter">
<a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266" class="">Initialization</a>
</li>
<li class="nav-chapter">
<a href="Deinitialization.html#TP40014097-CH19-XID_182" data-id="TP40014097-CH19-XID_182" class="">Deinitialization</a>
</li>
<li class="nav-chapter">
<a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50" class="">Automatic Reference Counting</a>
</li>
<li class="nav-chapter">
<a href="Optional Chaining.html#TP40014097-CH21-XID_312" data-id="TP40014097-CH21-XID_312" class="">Optional Chaining</a>
</li>
<li class="nav-chapter">
<a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443" class="">Type Casting</a>
</li>
<li class="nav-chapter">
<a href="Nested Types.html#TP40014097-CH23-XID_309" data-id="TP40014097-CH23-XID_309" class="">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191" class="">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345" class="">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234" class="">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28" class="">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name nav-part-active open-part">Language Reference
<ul class="nav-chapters" style="height: 299px;">
<li class="nav-chapter">
<a href="About the Language Reference.html#TP40014097-CH29-XID_453" data-id="TP40014097-CH29-XID_453" >About the Language Reference</a>
</li>
<li class="nav-chapter">
<a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794" class="">Lexical Structure</a>
</li>
<li class="nav-chapter">
<a href="Types.html#TP40014097-CH31-XID_988" data-id="TP40014097-CH31-XID_988" class="">Types</a>
</li>
<li class="nav-chapter">
<a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655" class="">Expressions</a>
</li>
<li class="nav-chapter">
<a href="Statements.html#TP40014097-CH33-XID_913" data-id="TP40014097-CH33-XID_913" class="">Statements</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="Declarations.html#TP40014097-CH34-XID_475" data-id="TP40014097-CH34-XID_475" class="nav-chapter-active">Declarations</a>
</li>
<li class="nav-chapter">
<a href="Attributes.html#TP40014097-CH35-XID_460" data-id="TP40014097-CH35-XID_460" class="">Attributes</a>
</li>
<li class="nav-chapter">
<a href="Patterns.html" 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-CH34"></a><a name="TP40014097-CH34-XID_475"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Declarations</h2>
<section role="navigation" class="hideInXcode" id="mini_toc">
<div id="mini_toc_button">
<p>On This Page</p>
</div>
<ul class="list-bullet">
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_494">
Module Scope
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_497">
Code Blocks
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_500">
Import Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_506">
Constant Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_512">
Variable Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_539">
Type Alias Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_545">
Function Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_566">
Enumeration Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_587">
Structure Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_592">
Class Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_597">
Protocol Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_625">
Initializer Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_630">
Deinitializer Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_633">
Extension Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_637">
Subscript Declaration
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH34-XID_644">
Operator Declaration
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
A <em>declaration</em> introduces a new name or construct into your program. For example, you use declarations to introduce functions and methods, variables and constants, and to define new, named enumeration, structure, class, and protocol types. You can also use a declaration to extend the the behavior of an existing named type and to import symbols into your program that are declared elsewhere.
</p>
<p class="para">
In Swift, most declarations are also definitions in the sense that they are implemented or initialized at the same time they are declared. That said, because protocols don’t implement their members, most protocol members are declarations only. For convenience and because the distinction isn’t that important in Swift, the term <em>declaration</em> covers both declarations and definitions.
</p>
<div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a declaration
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/declaration"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/import-declaration" href="#//apple_ref/swift/grammar/import-declaration">import-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_478"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/constant-declaration" href="#//apple_ref/swift/grammar/constant-declaration">constant-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_479"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-declaration" href="#//apple_ref/swift/grammar/variable-declaration">variable-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_480"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/typealias-declaration" href="#//apple_ref/swift/grammar/typealias-declaration">typealias-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_481"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/function-declaration" href="#//apple_ref/swift/grammar/function-declaration">function-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_482"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/enum-declaration" href="#//apple_ref/swift/grammar/enum-declaration">enum-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_483"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/struct-declaration" href="#//apple_ref/swift/grammar/struct-declaration">struct-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_484"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/class-declaration" href="#//apple_ref/swift/grammar/class-declaration">class-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_485"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/protocol-declaration" href="#//apple_ref/swift/grammar/protocol-declaration">protocol-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_486"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/initializer-declaration" href="#//apple_ref/swift/grammar/initializer-declaration">initializer-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_487"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/deinitializer-declaration" href="#//apple_ref/swift/grammar/deinitializer-declaration">deinitializer-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_488"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/extension-declaration" href="#//apple_ref/swift/grammar/extension-declaration">extension-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_489"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/subscript-declaration" href="#//apple_ref/swift/grammar/subscript-declaration">subscript-declaration</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_490"></a>
<span class="syntax-def-name">
declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/operator-declaration" href="#//apple_ref/swift/grammar/operator-declaration">operator-declaration</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/declarations"></a>
<span class="syntax-def-name">
declarations
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/declaration" href="#//apple_ref/swift/grammar/declaration">declaration</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/declarations" href="#//apple_ref/swift/grammar/declarations">declarations</a></span><sub class="subscript">opt</sub></span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/declaration-specifiers"></a>
<span class="syntax-def-name">
declaration-specifiers
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/declaration-specifier" href="#//apple_ref/swift/grammar/declaration-specifier">declaration-specifier</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/declaration-specifiers" href="#//apple_ref/swift/grammar/declaration-specifiers">declaration-specifiers</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/declaration-specifier"></a>
<span class="syntax-def-name">
declaration-specifier
</span>
<span class="arrow">
→
</span><span class="alternative">
<code class="literal">class</code>
</span><span class="alternative">
<code class="literal">mutating</code>
</span><span class="alternative">
<code class="literal">nonmutating</code>
</span><span class="alternative">
<code class="literal">override</code>
</span><span class="alternative">
<code class="literal">static</code>
</span><span class="alternative">
<code class="literal">unowned</code>
</span><span class="alternative">
<code class="literal">unowned(safe)</code>
</span><span class="alternative">
<code class="literal">unowned(unsafe)</code>
</span><span class="alternative">
<code class="literal">weak</code>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_494"></a>
<h3 tabindex="0" class="section-name">Module Scope</h3>
<p class="para">
The module scope defines the code that’s visible to other code in Swift source files that are part of the same module. The top-level code in a Swift source file consists of zero or more statements, declarations, and expressions. Variables, constants, and other named declarations that are declared at the top-level of a source file are visible to code in every source file that is part of the same module.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a top-level declaration
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/top-level-declaration"></a>
<span class="syntax-def-name">
top-level-declaration
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/statements" href="Statements.html#//apple_ref/swift/grammar/statements">statements</a></span><sub class="subscript">opt</sub></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_497"></a>
<h3 tabindex="0" class="section-name">Code Blocks</h3>
<p class="para">
A <em>code block</em> is used by a variety of declarations and control structures to group statements together. It has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice">{</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
The <em>statements</em> inside a code block include declarations, expressions, and other kinds of statements and are executed in order of their appearance in source code.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a code block
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/code-block"></a>
<span class="syntax-def-name">
code-block
</span>
<span class="arrow">
→
</span><code class="literal">{</code><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/statements" href="Statements.html#//apple_ref/swift/grammar/statements">statements</a></span><sub class="subscript">opt</sub></span><code class="literal">}</code>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_500"></a>
<h3 tabindex="0" class="section-name">Import Declaration</h3>
<p class="para">
An <em>import declaration</em> lets you access symbols that are declared outside the current file. The basic form imports the entire module; it consists of the <code class="code-voice">import</code> keyword followed by a module name:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">import</span> <em class="variable-text">module</em></pre></li>
</ul>
</div><p class="para">
Providing more detail limits which symbols are imported—you can specify a specific submodule or a specific declaration within a module or submodule. When this detailed form is used, only the imported symbol (and not the module that declares it) is made available in the current scope.
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">import</span> <em class="variable-text">import kind</em> <em class="variable-text">module</em>.<em class="variable-text">symbol name</em></pre></li><li><pre class="code-voice"><span class="kt">import</span> <em class="variable-text">module</em>.<em class="variable-text">submodule</em></pre></li>
</ul>
</div><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an import declaration
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/import-declaration"></a>
<span class="syntax-def-name">
import-declaration
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><code class="literal">import</code><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/import-kind" href="#//apple_ref/swift/grammar/import-kind">import-kind</a></span><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/import-path" href="#//apple_ref/swift/grammar/import-path">import-path</a></span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/import-kind"></a>
<span class="syntax-def-name">
import-kind
</span>
<span class="arrow">
→
</span><span class="alternative">
<code class="literal">typealias</code>
</span><span class="alternative">
<code class="literal">struct</code>
</span><span class="alternative">
<code class="literal">class</code>
</span><span class="alternative">
<code class="literal">enum</code>
</span><span class="alternative">
<code class="literal">protocol</code>
</span><span class="alternative">
<code class="literal">var</code>
</span><span class="alternative">
<code class="literal">func</code>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/import-path"></a>
<span class="syntax-def-name">
import-path
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/import-path-identifier" href="#//apple_ref/swift/grammar/import-path-identifier">import-path-identifier</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/import-path-identifier" href="#//apple_ref/swift/grammar/import-path-identifier">import-path-identifier</a></span><code class="literal">.</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/import-path" href="#//apple_ref/swift/grammar/import-path">import-path</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/import-path-identifier"></a>
<span class="syntax-def-name">
import-path-identifier
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/identifier" href="Lexical Structure.html#//apple_ref/swift/grammar/identifier">identifier</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/operator" href="Lexical Structure.html#//apple_ref/swift/grammar/operator">operator</a></span>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_506"></a>
<h3 tabindex="0" class="section-name">Constant Declaration</h3>
<p class="para">
A <em>constant declaration</em> introduces a constant named value into your program. Constant declarations are declared using the keyword <code class="code-voice">let</code> and have the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">let</span> <em class="variable-text">constant name</em>: <em class="variable-text">type</em> = <em class="variable-text">expression</em></pre></li>
</ul>
</div><p class="para">
A constant declaration defines an immutable binding between the <em>constant name</em> and the value of the initializer <em>expression</em>; after the value of a constant is set, it cannot be changed. That said, if a constant is initialized with a class object, the object itself can change, but the binding between the constant name and the object it refers to can’t.
</p><p class="para">
When a constant is declared at global scope, it must be initialized with a value. When a constant declaration occurs in the context of a class or structure declaration, it is considered a <em>constant property</em>. Constant declarations are not computed properties and therefore do not have getters or setters.
</p><p class="para">
If the <em>constant name</em> of a constant declaration is a tuple pattern, the name of each item in the tuple is bound to the corresponding value in the initializer <em>expression</em>.
</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">firstNumber</span>, <span class="vc">secondNumber</span>) = (<span class="m">10</span>, <span class="m">42</span>)</code></li>
</ul>
</div>
</section><p class="para">
In this example, <code class="code-voice">firstNumber</code> is a named constant for the value <code class="code-voice">10</code>, and <code class="code-voice">secondNumber</code> is a named constant for the value <code class="code-voice">42</code>. Both constants can now be used independently:
</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 first number is </span>\(<span class="vc">firstNumber</span>)<span class="s">."</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "The first number is 10."</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"The second number is </span>\(<span class="vc">secondNumber</span>)<span class="s">."</span>)</code></li>
<li><code class="code-voice"><span class="c">// prints "The second number is 42."</span></code></li>
</ul>
</div>
</section><p class="para">
The type annotation (<code class="code-voice">:</code> <em>type</em>) is optional in a constant declaration when the type of the <em>constant name</em> can be inferred, as described in <span class="x-name"><a data-id="TP40014097-CH31-XID_1029" href="Types.html#TP40014097-CH31-XID_1029">Type Inference</a></span>.
</p><p class="para">
To declare a static constant property, mark the declaration with the <code class="code-voice">static</code> keyword. Static properties are discussed in <span class="x-name"><a data-id="TP40014097-CH14-XID_339" href="Properties.html#TP40014097-CH14-XID_339">Type Properties</a></span>.
</p><p class="para">
For more information about constants and for guidance about when to use them, see <span class="x-name"><a data-id="TP40014097-CH5-XID_400" href="The Basics.html#TP40014097-CH5-XID_400">Constants and Variables</a></span> and <span class="x-name"><a data-id="TP40014097-CH14-XID_324" href="Properties.html#TP40014097-CH14-XID_324">Stored Properties</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a constant declaration
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/constant-declaration"></a>
<span class="syntax-def-name">
constant-declaration
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/declaration-specifiers" href="#//apple_ref/swift/grammar/declaration-specifiers">declaration-specifiers</a></span><sub class="subscript">opt</sub></span><code class="literal">let</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern-initializer-list" href="#//apple_ref/swift/grammar/pattern-initializer-list">pattern-initializer-list</a></span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/pattern-initializer-list"></a>
<span class="syntax-def-name">
pattern-initializer-list
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern-initializer" href="#//apple_ref/swift/grammar/pattern-initializer">pattern-initializer</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern-initializer" href="#//apple_ref/swift/grammar/pattern-initializer">pattern-initializer</a></span><code class="literal">,</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern-initializer-list" href="#//apple_ref/swift/grammar/pattern-initializer-list">pattern-initializer-list</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/pattern-initializer"></a>
<span class="syntax-def-name">
pattern-initializer
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern" href="Patterns.html#//apple_ref/swift/grammar/pattern">pattern</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/initializer" href="#//apple_ref/swift/grammar/initializer">initializer</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/initializer"></a>
<span class="syntax-def-name">
initializer
</span>
<span class="arrow">
→
</span><code class="literal">=</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/expression" href="Expressions.html#//apple_ref/swift/grammar/expression">expression</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_512"></a>
<h3 tabindex="0" class="section-name">Variable Declaration</h3>
<p class="para">
A <em>variable declaration</em> introduces a variable named value into your program and is declared using the keyword <code class="code-voice">var</code>.
</p><p class="para">
Variable declarations have several forms that declare different kinds of named, mutable values, including stored and computed variables and properties, stored variable and property observers, and static variable properties. The appropriate form to use depends on the scope at which the variable is declared and the kind of variable you intend to declare.
</p><div class="note">
<a name="TP40014097-CH34-XID_513"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">You can also declare properties in the context of a protocol declaration, as described in <span class="x-name"><a data-id="TP40014097-CH34-XID_610" href="#TP40014097-CH34-XID_610">Protocol Property Declaration</a></span>.
</p>
</aside>
</div><p class="para">
You can override a property in a subclass by prefixing the subclass’s property declaration with the <code class="code-voice">override</code> keyword, as described in <span class="x-name"><a data-id="TP40014097-CH17-XID_257" href="Inheritance.html#TP40014097-CH17-XID_257">Overriding</a></span>.
</p>
<section class="section">
<a name="TP40014097-CH34-XID_514"></a>
<h3 tabindex="0" class="section-name">Stored Variables and Stored Variable Properties</h3>
<p class="para">
The following form declares a stored variable or stored variable property:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">var</span> <em class="variable-text">variable name</em>: <em class="variable-text">type</em> = <em class="variable-text">expression</em></pre></li>
</ul>
</div><p class="para">
You define this form of a variable declaration at global scope, the local scope of a function, or in the context of a class or structure declaration. When a variable declaration of this form is declared at global scope or the local scope of a function, it is referred to as a <em>stored variable</em>. When it is declared in the context of a class or structure declaration, it is referred to as a <em>stored variable property</em>.
</p><p class="para">
The initializer <em>expression</em> can’t be present in a protocol declaration, but in all other contexts, the initializer <em>expression</em> is optional. That said, if no initializer <em>expression</em> is present, the variable declaration must include an explicit type annotation (<code class="code-voice">:</code> <em>type</em>).
</p><p class="para">
As with constant declarations, if the <em>variable name</em> is a tuple pattern, the name of each item in the tuple is bound to the corresponding value in the initializer <em>expression</em>.
</p><p class="para">
As their names suggest, the value of a stored variable or a stored variable property is stored in memory.
</p>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_515"></a>
<h3 tabindex="0" class="section-name">Computed Variables and Computed Properties</h3>
<p class="para">
The following form declares a computed variable or computed property:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">var</span> <em class="variable-text">variable name</em>: <em class="variable-text">type</em> {</pre></li><li><pre class="code-voice"><span class="kt">get</span> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li><li><pre class="code-voice"><span class="kt">set</span>(<em class="variable-text">setter name</em>) {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
You define this form of a variable declaration at global scope, the local scope of a function, or in the context of a class, structure, enumeration, or extension declaration. When a variable declaration of this form is declared at global scope or the local scope of a function, it is referred to as a <em>computed variable</em>. When it is declared in the context of a class, structure, or extension declaration, it is referred to as a <em>computed property</em>.
</p><p class="para">
The getter is used to read the value, and the setter is used to write the value. The setter clause is optional, and when only a getter is needed, you can omit both clauses and simply return the requested value directly, as described in <span class="x-name"><a data-id="TP40014097-CH14-XID_331" href="Properties.html#TP40014097-CH14-XID_331">Read-Only Computed Properties</a></span>. But if you provide a setter clause, you must also provide a getter clause.
</p><p class="para">
The <em>setter name</em> and enclosing parentheses is optional. If you provide a setter name, it is used as the name of the parameter to the setter. If you do not provide a setter name, the default parameter name to the setter is <code class="code-voice">newValue</code>, as described in <span class="x-name"><a data-id="TP40014097-CH14-XID_330" href="Properties.html#TP40014097-CH14-XID_330">Shorthand Setter Declaration</a></span>.
</p><p class="para">
Unlike stored named values and stored variable properties, the value of a computed named value or a computed property is not stored in memory.
</p><p class="para">
For more information and to see examples of computed properties, see <span class="x-name"><a data-id="TP40014097-CH14-XID_329" href="Properties.html#TP40014097-CH14-XID_329">Computed Properties</a></span>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_516"></a>
<h3 tabindex="0" class="section-name">Stored Variable Observers and Property Observers</h3>
<p class="para">
You can also declare a stored variable or property with <code class="code-voice">willSet</code> and <code class="code-voice">didSet</code> observers. A stored variable or property declared with observers has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">var</span> <em class="variable-text">variable name</em>: <em class="variable-text">type</em> = <em class="variable-text">expression</em> {</pre></li><li><pre class="code-voice"><span class="kt">willSet</span>(<em class="variable-text">setter name</em>) {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li><li><pre class="code-voice"><span class="kt">didSet</span>(<em class="variable-text">setter name</em> {</pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
You define this form of a variable declaration at global scope, the local scope of a function, or in the context of a class or structure declaration. When a variable declaration of this form is declared at global scope or the local scope of a function, the observers are referred to as <em>stored variable observers</em>. When it is declared in the context of a class or structure declaration, the observers are referred to as <em>property observers</em>.
</p><p class="para">
You can add property observers to any stored property. You can also add property observers to any inherited property (whether stored or computed) by overriding the property within a subclass, as described in <span class="x-name"><a data-id="TP40014097-CH17-XID_263" href="Inheritance.html#TP40014097-CH17-XID_263">Overriding Property Observers</a></span>.
</p><p class="para">
The initializer <em>expression</em> is optional in the context of a class or structure declaration, but required elsewhere. The type annotation is required in all variable declarations that include observers, regardless of the context in which they are declared.
</p><p class="para">
The <code class="code-voice">willSet</code> and <code class="code-voice">didSet</code> observers provide a way to observe (and to respond appropriately) when the value of a variable or property is being set. The observers are not called when the variable or property is first initialized. Instead, they are called only when the value is set outside of an initialization context.
</p><p class="para">
A <code class="code-voice">willSet</code> observer is called just before the value of the variable or property is set. The new value is passed to the <code class="code-voice">willSet</code> observer as a constant, and therefore it can’t be changed in the implementation of the <code class="code-voice">willSet</code> clause. The <code class="code-voice">didSet</code> observer is called immediately after the new value is set. In contrast to the <code class="code-voice">willSet</code> observer, the old value of the variable or property is passed to the <code class="code-voice">didSet</code> observer in case you still need access to it. That said, if you assign a value to a variable or property within its own <code class="code-voice">didSet</code> observer clause, that new value that you assign will replace the one that was just set and passed to the <code class="code-voice">willSet</code> observer.
</p><p class="para">
The <em>setter name</em> and enclosing parentheses in the <code class="code-voice">willSet</code> and <code class="code-voice">didSet</code> clauses are optional. If you provide setter names, they are used as the parameter names to the <code class="code-voice">willSet</code> and <code class="code-voice">didSet</code> observers. If you do not provide setter names, the default parameter name to the <code class="code-voice">willSet</code> observer is <code class="code-voice">newValue</code> and the default parameter name to the <code class="code-voice">didSet</code> observer is <code class="code-voice">oldValue</code>.
</p><p class="para">
The <code class="code-voice">didSet</code> clause is optional when you provide a <code class="code-voice">willSet</code> clause. Likewise, the <code class="code-voice">willSet</code> clause is optional when you provide a <code class="code-voice">didSet</code> clause.
</p><p class="para">
For more information and to see an example of how to use property observers, see <span class="x-name"><a data-id="TP40014097-CH14-XID_333" href="Properties.html#TP40014097-CH14-XID_333">Property Observers</a></span>.
</p>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_517"></a>
<h3 tabindex="0" class="section-name">Class and Static Variable Properties</h3>
<p class="para">
To declare a class computed property, mark the declaration with the <code class="code-voice">class</code> keyword. To declare a static variable property, mark the declaration with the <code class="code-voice">static</code> keyword. Class and static properties are discussed in <span class="x-name"><a data-id="TP40014097-CH14-XID_339" href="Properties.html#TP40014097-CH14-XID_339">Type Properties</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a variable declaration
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/variable-declaration"></a>
<span class="syntax-def-name">
variable-declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-declaration-head" href="#//apple_ref/swift/grammar/variable-declaration-head">variable-declaration-head</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/pattern-initializer-list" href="#//apple_ref/swift/grammar/pattern-initializer-list">pattern-initializer-list</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_520"></a>
<span class="syntax-def-name">
variable-declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-declaration-head" href="#//apple_ref/swift/grammar/variable-declaration-head">variable-declaration-head</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-name" href="#//apple_ref/swift/grammar/variable-name">variable-name</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-annotation" href="Types.html#//apple_ref/swift/grammar/type-annotation">type-annotation</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/code-block" href="#//apple_ref/swift/grammar/code-block">code-block</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_521"></a>
<span class="syntax-def-name">
variable-declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-declaration-head" href="#//apple_ref/swift/grammar/variable-declaration-head">variable-declaration-head</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-name" href="#//apple_ref/swift/grammar/variable-name">variable-name</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-annotation" href="Types.html#//apple_ref/swift/grammar/type-annotation">type-annotation</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/getter-setter-block" href="#//apple_ref/swift/grammar/getter-setter-block">getter-setter-block</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_522"></a>
<span class="syntax-def-name">
variable-declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-declaration-head" href="#//apple_ref/swift/grammar/variable-declaration-head">variable-declaration-head</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-name" href="#//apple_ref/swift/grammar/variable-name">variable-name</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-annotation" href="Types.html#//apple_ref/swift/grammar/type-annotation">type-annotation</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/getter-setter-keyword-block" href="#//apple_ref/swift/grammar/getter-setter-keyword-block">getter-setter-keyword-block</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_523"></a>
<span class="syntax-def-name">
variable-declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-declaration-head" href="#//apple_ref/swift/grammar/variable-declaration-head">variable-declaration-head</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/variable-name" href="#//apple_ref/swift/grammar/variable-name">variable-name</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/type-annotation" href="Types.html#//apple_ref/swift/grammar/type-annotation">type-annotation</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/initializer" href="#//apple_ref/swift/grammar/initializer">initializer</a></span><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/willSet-didSet-block" href="#//apple_ref/swift/grammar/willSet-didSet-block">willSet-didSet-block</a></span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/variable-declaration-head"></a>
<span class="syntax-def-name">
variable-declaration-head
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/declaration-specifiers" href="#//apple_ref/swift/grammar/declaration-specifiers">declaration-specifiers</a></span><sub class="subscript">opt</sub></span><code class="literal">var</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/variable-name"></a>
<span class="syntax-def-name">
variable-name
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/identifier" href="Lexical Structure.html#//apple_ref/swift/grammar/identifier">identifier</a></span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/getter-setter-block"></a>
<span class="syntax-def-name">
getter-setter-block
</span>
<span class="arrow">
→
</span><code class="literal">{</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/getter-clause" href="#//apple_ref/swift/grammar/getter-clause">getter-clause</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/setter-clause" href="#//apple_ref/swift/grammar/setter-clause">setter-clause</a></span><sub class="subscript">opt</sub></span><code class="literal">}</code>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_527"></a>
<span class="syntax-def-name">
getter-setter-block
</span>
<span class="arrow">
→
</span><code class="literal">{</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/setter-clause" href="#//apple_ref/swift/grammar/setter-clause">setter-clause</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/getter-clause" href="#//apple_ref/swift/grammar/getter-clause">getter-clause</a></span><code class="literal">}</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/getter-clause"></a>
<span class="syntax-def-name">
getter-clause
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><code class="literal">get</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/code-block" href="#//apple_ref/swift/grammar/code-block">code-block</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/setter-clause"></a>
<span class="syntax-def-name">
setter-clause
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><code class="literal">set</code><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/setter-name" href="#//apple_ref/swift/grammar/setter-name">setter-name</a></span><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/code-block" href="#//apple_ref/swift/grammar/code-block">code-block</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/setter-name"></a>
<span class="syntax-def-name">
setter-name
</span>
<span class="arrow">
→
</span><code class="literal">(</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/identifier" href="Lexical Structure.html#//apple_ref/swift/grammar/identifier">identifier</a></span><code class="literal">)</code>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/getter-setter-keyword-block"></a>
<span class="syntax-def-name">
getter-setter-keyword-block
</span>
<span class="arrow">
→
</span><code class="literal">{</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/getter-keyword-clause" href="#//apple_ref/swift/grammar/getter-keyword-clause">getter-keyword-clause</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/setter-keyword-clause" href="#//apple_ref/swift/grammar/setter-keyword-clause">setter-keyword-clause</a></span><sub class="subscript">opt</sub></span><code class="literal">}</code>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_532"></a>
<span class="syntax-def-name">
getter-setter-keyword-block
</span>
<span class="arrow">
→
</span><code class="literal">{</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/setter-keyword-clause" href="#//apple_ref/swift/grammar/setter-keyword-clause">setter-keyword-clause</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/getter-keyword-clause" href="#//apple_ref/swift/grammar/getter-keyword-clause">getter-keyword-clause</a></span><code class="literal">}</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/getter-keyword-clause"></a>
<span class="syntax-def-name">
getter-keyword-clause
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><code class="literal">get</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/setter-keyword-clause"></a>
<span class="syntax-def-name">
setter-keyword-clause
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><code class="literal">set</code>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/willSet-didSet-block"></a>
<span class="syntax-def-name">
willSet-didSet-block
</span>
<span class="arrow">
→
</span><code class="literal">{</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/willSet-clause" href="#//apple_ref/swift/grammar/willSet-clause">willSet-clause</a></span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/didSet-clause" href="#//apple_ref/swift/grammar/didSet-clause">didSet-clause</a></span><sub class="subscript">opt</sub></span><code class="literal">}</code>
</p><p class="syntax-def">
<a name="TP40014097-CH34-XID_536"></a>
<span class="syntax-def-name">
willSet-didSet-block
</span>
<span class="arrow">
→
</span><code class="literal">{</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/didSet-clause" href="#//apple_ref/swift/grammar/didSet-clause">didSet-clause</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/willSet-clause" href="#//apple_ref/swift/grammar/willSet-clause">willSet-clause</a></span><code class="literal">}</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/willSet-clause"></a>
<span class="syntax-def-name">
willSet-clause
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><code class="literal">willSet</code><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/setter-name" href="#//apple_ref/swift/grammar/setter-name">setter-name</a></span><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/code-block" href="#//apple_ref/swift/grammar/code-block">code-block</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/didSet-clause"></a>
<span class="syntax-def-name">
didSet-clause
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/attributes" href="Attributes.html#//apple_ref/swift/grammar/attributes">attributes</a></span><sub class="subscript">opt</sub></span><code class="literal">didSet</code><span class="optional"><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/setter-name" href="#//apple_ref/swift/grammar/setter-name">setter-name</a></span><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/code-block" href="#//apple_ref/swift/grammar/code-block">code-block</a></span>
</p>
</div>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH34-XID_539"></a>
<h3 tabindex="0" class="section-name">Type Alias Declaration</h3>
<p class="para">
A <em>type alias declaration</em> introduces a named alias of an existing type into your program. Type alias declarations begin with the keyword <code class="code-voice">typealias</code> and have the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">typealias</span> <em class="variable-text">name</em> = <em class="variable-text">existing type</em></pre></li>
</ul>
</div><p class="para">
After a type alias is declared, the aliased <em>name</em> can be used instead of the <em>existing type</em> everywhere in your program. The <em>existing type</em> can be a named type or a compound type. Type aliases do not create new types; they simply allow a name to refer to an existing type.
</p><p class="para">
See also <span class="x-name"><a data-id="TP40014097-CH34-XID_622" href="#TP40014097-CH34-XID_622">Protocol Associated Type Declaration</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a type alias declaration
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/typealias-declaration"></a>
<span class="syntax-def-name">
typealias-declaration
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/typealias-head" href="#//apple_ref/swift/grammar/typealias-head">typealias-head</a></span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/typealias-assignment" href="#//apple_ref/swift/grammar/typealias-assignment">typealias-assignment</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/typealias-head"></a>
<span class="syntax-def-name">
typealias-head
</span>
<span class="arrow">
→
</span><code class="literal">typealias</code><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/typealias-name" href="#//apple_ref/swift/grammar/typealias-name">typealias-name</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/typealias-name"></a>
<span class="syntax-def-name">
typealias-name
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a data-id="//apple_ref/swift/grammar/identifier" href="Lexical Structure.html#//apple_ref/swift/grammar/identifier">identifier</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/typealias-assignment"></a>