-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressions.html
More file actions
1991 lines (1845 loc) · 104 KB
/
Copy pathExpressions.html
File metadata and controls
1991 lines (1845 loc) · 104 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: Expressions</title>
<link rel="stylesheet" type="text/css" href="resource/style-1.1.15.css">
<meta charset="utf-8"> <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
<body id="conceptual_flow_with_tasks" class="jazz">
<div id="_omniture_top">
</div>
<a name="TP40014097" title="The Swift Programming Language"></a>
<section id="valence">
<div class="content-wrapper">
<p id="hierarchial_navigation">
<span id="book_title">The Swift Programming Language</span>
</p>
<img id="shortstack" src="./resource/shortstack_2x.png">
</div>
</section>
<div class="content-wrapper">
<nav class="book-parts hideInXcode" role="navigation">
<ul class="nav-parts">
<li data-id="TP40014097-CH1-XID_27" class="part-name">Welcome to Swift
<ul class="nav-chapters">
<li class="nav-chapter nav-visited-chapter">
<a href="About Swift.html#TP40014097-CH3-XID_0" data-id="TP40014097-CH3-XID_0" class="">About Swift</a>
</li>
<li class="nav-chapter">
<a href="A Swift Tour.html#TP40014097-CH2-XID_1" data-id="TP40014097-CH2-XID_1" class="">A Swift Tour</a>
</li>
</ul>
</li><li data-id="TP40014097-CH4-XID_299" class="part-name">Language Guide
<ul class="nav-chapters">
<li class="nav-chapter">
<a href="The Basics.html#TP40014097-CH5-XID_399" data-id="TP40014097-CH5-XID_399" class="">The Basics</a>
</li>
<li class="nav-chapter">
<a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70" class="">Basic Operators</a>
</li>
<li class="nav-chapter">
<a href="Strings and Characters.html#TP40014097-CH7-XID_368" data-id="TP40014097-CH7-XID_368" class="">Strings and Characters</a>
</li>
<li class="nav-chapter">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133" class="">Collection Types</a>
</li>
<li class="nav-chapter">
<a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153" class="">Control Flow</a>
</li>
<li class="nav-chapter">
<a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204" class="">Functions</a>
</li>
<li class="nav-chapter">
<a href="Closures.html#TP40014097-CH11-XID_117" data-id="TP40014097-CH11-XID_117" class="">Closures</a>
</li>
<li class="nav-chapter">
<a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185" class="">Enumerations</a>
</li>
<li class="nav-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94" class="">Classes and Structures</a>
</li>
<li class="nav-chapter">
<a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323" class="">Properties</a>
</li>
<li class="nav-chapter">
<a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300" class="">Methods</a>
</li>
<li class="nav-chapter">
<a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393" class="">Subscripts</a>
</li>
<li class="nav-chapter">
<a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251" class="">Inheritance</a>
</li>
<li class="nav-chapter">
<a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266" class="">Initialization</a>
</li>
<li class="nav-chapter">
<a href="Deinitialization.html#TP40014097-CH19-XID_182" data-id="TP40014097-CH19-XID_182" class="">Deinitialization</a>
</li>
<li class="nav-chapter">
<a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50" class="">Automatic Reference Counting</a>
</li>
<li class="nav-chapter">
<a href="Optional Chaining.html#TP40014097-CH21-XID_312" data-id="TP40014097-CH21-XID_312" class="">Optional Chaining</a>
</li>
<li class="nav-chapter">
<a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443" class="">Type Casting</a>
</li>
<li class="nav-chapter">
<a href="Nested Types.html#TP40014097-CH23-XID_309" data-id="TP40014097-CH23-XID_309" class="">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191" class="">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345" class="">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234" class="">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28" class="">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name nav-part-active open-part">Language Reference
<ul class="nav-chapters" style="height: 299px;">
<li class="nav-chapter">
<a href="About the Language Reference.html#TP40014097-CH29-XID_453" data-id="TP40014097-CH29-XID_453" class="">About the Language Reference</a>
</li>
<li class="nav-chapter">
<a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794" class="">Lexical Structure</a>
</li>
<li class="nav-chapter">
<a href="Types.html#TP40014097-CH31-XID_988" data-id="TP40014097-CH31-XID_988" class="">Types</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="Expressions.html" data-id="TP40014097-CH32-XID_655" class="nav-chapter-active">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-CH32"></a><a name="TP40014097-CH32-XID_655"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Expressions</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-CH32-XID_659">
Prefix Expressions
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH32-XID_664">
Binary Expressions
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH32-XID_681">
Primary Expressions
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH32-XID_735">
Postfix Expressions
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
In Swift, there are four kinds of expressions: prefix expressions, binary expressions, primary expressions, and postfix expressions. Evaluating an expression returns a value, causes a side effect, or both.
</p>
<p class="para">
Prefix and binary expressions let you apply operators to smaller expressions. Primary expressions are conceptually the simplest kind of expression, and they provide a way to access values. Postfix expressions, like prefix and binary expressions, let you build up more complex expressions using postfixes such as function calls and member access. Each kind of expression is described in detail in the sections below.
</p>
<div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an expression
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/expression"></a>
<span class="syntax-def-name">
expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/prefix-expression" data-id="//apple_ref/swift/grammar/prefix-expression">prefix-expression</a></span><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/binary-expressions" data-id="//apple_ref/swift/grammar/binary-expressions">binary-expressions</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/expression-list"></a>
<span class="syntax-def-name">
expression-list
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><code class="literal">,</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/expression-list" data-id="//apple_ref/swift/grammar/expression-list">expression-list</a></span>
</span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH32-XID_659"></a>
<h3 class="section-name" tabindex="0">Prefix Expressions</h3>
<p class="para">
<em>Prefix expressions</em> combine an optional prefix operator with an expression. Prefix operators take one argument, the expression that follows them.
</p><p class="para">
The Swift standard library provides the following prefix operators:
</p><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">++</code> Increment
</p>
</li><li class="item"><p class="para">
<code class="code-voice">--</code> Decrement
</p>
</li><li class="item"><p class="para">
<code class="code-voice">!</code> Logical NOT
</p>
</li><li class="item"><p class="para">
<code class="code-voice">~</code> Bitwise NOT
</p>
</li><li class="item"><p class="para">
<code class="code-voice">+</code> Unary plus
</p>
</li><li class="item"><p class="para">
<code class="code-voice">-</code> Unary minus
</p>
</li>
</ul><p class="para">
For information about the behavior of these operators, see <span class="x-name"><a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70">Basic Operators</a></span> and <span class="x-name"><a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28">Advanced Operators</a></span>.
</p><p class="para">
In addition to the standard library operators listed above, you use <code class="code-voice">&</code> immediately before the name of a variable that’s being passed as an in-out argument to a function call expression. For more information and to see an example, see <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_226" data-id="TP40014097-CH10-XID_226">In-Out Parameters</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a prefix expression
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/prefix-expression"></a>
<span class="syntax-def-name">
prefix-expression
</span>
<span class="arrow">
→
</span><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/prefix-operator" data-id="//apple_ref/swift/grammar/prefix-operator">prefix-operator</a></span><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/postfix-expression" data-id="//apple_ref/swift/grammar/postfix-expression">postfix-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_662"></a>
<span class="syntax-def-name">
prefix-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/in-out-expression" data-id="//apple_ref/swift/grammar/in-out-expression">in-out-expression</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/in-out-expression"></a>
<span class="syntax-def-name">
in-out-expression
</span>
<span class="arrow">
→
</span><code class="literal">&</code><span class="syntactic-cat"><a href="Lexical Structure.html#//apple_ref/swift/grammar/identifier" data-id="//apple_ref/swift/grammar/identifier">identifier</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH32-XID_664"></a>
<h3 class="section-name" tabindex="0">Binary Expressions</h3>
<p class="para">
<em>Binary expressions</em> combine an infix binary operator with the expression that it takes as its left-hand and right-hand arguments. It has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><em class="variable-text">left-hand argument</em> <em class="variable-text">operator</em> <em class="variable-text">right-hand argument</em></pre></li>
</ul>
</div><p class="para">
The Swift standard library provides the following binary operators:
</p><ul class="list-bullet">
<li class="item"><dl class="term-definition termdef">
<dt class="term">Exponentiative (No associativity, precedence level 160)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice"><<</code> Bitwise left shift
</p>
</li><li class="item"><p class="para">
<code class="code-voice">>></code> Bitwise right shift
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Multiplicative (Left associative, precedence level 150)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">*</code> Multiply
</p>
</li><li class="item"><p class="para">
<code class="code-voice">/</code> Divide
</p>
</li><li class="item"><p class="para">
<code class="code-voice">%</code> Remainder
</p>
</li><li class="item"><p class="para">
<code class="code-voice">&*</code> Multiply, ignoring overflow
</p>
</li><li class="item"><p class="para">
<code class="code-voice">&/</code> Divide, ignoring overflow
</p>
</li><li class="item"><p class="para">
<code class="code-voice">&%</code> Remainder, ignoring overflow
</p>
</li><li class="item"><p class="para">
<code class="code-voice">&</code> Bitwise AND
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Additive (Left associative, precedence level 140)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">+</code> Add
</p>
</li><li class="item"><p class="para">
<code class="code-voice">-</code> Subtract
</p>
</li><li class="item"><p class="para">
<code class="code-voice">&+</code> Add with overflow
</p>
</li><li class="item"><p class="para">
<code class="code-voice">&-</code> Subtract with overflow
</p>
</li><li class="item"><p class="para">
<code class="code-voice">|</code> Bitwise OR
</p>
</li><li class="item"><p class="para">
<code class="code-voice">^</code> Bitwise XOR
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Range (No associativity, precedence level 135)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">..</code> Half-closed range
</p>
</li><li class="item"><p class="para">
<code class="code-voice">...</code> Closed range
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Cast (No associativity, precedence level 132)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">is</code> Type check
</p>
</li><li class="item"><p class="para">
<code class="code-voice">as</code> Type cast
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Comparative (No associativity, precedence level 130)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice"><</code> Less than
</p>
</li><li class="item"><p class="para">
<code class="code-voice"><=</code> Less than or equal
</p>
</li><li class="item"><p class="para">
<code class="code-voice">></code> Greater than
</p>
</li><li class="item"><p class="para">
<code class="code-voice">>=</code> Greater than or equal
</p>
</li><li class="item"><p class="para">
<code class="code-voice">==</code> Equal
</p>
</li><li class="item"><p class="para">
<code class="code-voice">!=</code> Not equal
</p>
</li><li class="item"><p class="para">
<code class="code-voice">===</code> Identical
</p>
</li><li class="item"><p class="para">
<code class="code-voice">!==</code> Not identical
</p>
</li><li class="item"><p class="para">
<code class="code-voice">~=</code> Pattern match
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Conjunctive (Left associative, precedence level 120)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">&&</code> Logical AND
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Disjunctive (Left associative, precedence level 110)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">||</code> Logical OR
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Ternary Conditional (Right associative, precedence level 100)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">?</code><code class="code-voice">:</code> Ternary conditional
</p>
</li>
</ul></dd>
</dl></li><li class="item"><dl class="term-definition termdef">
<dt class="term">Assignment (Right associative, precedence level 90)</dt><dd class="definition"><ul class="list-bullet">
<li class="item"><p class="para">
<code class="code-voice">=</code> Assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">*=</code> Multiply and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">/=</code> Divide and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">%=</code> Remainder and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">+=</code> Add and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">-=</code> Subtract and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice"><<=</code> Left bit shift and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">>>=</code> Right bit shift and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">&=</code> Bitwise AND and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">^=</code> Bitwise XOR and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">|=</code> Bitwise OR and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">&&=</code> Logical AND and assign
</p>
</li><li class="item"><p class="para">
<code class="code-voice">||=</code> Logical OR and assign
</p>
</li>
</ul></dd>
</dl></li>
</ul><p class="para">
For information about the behavior of these operators, see <span class="x-name"><a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70">Basic Operators</a></span> and <span class="x-name"><a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28">Advanced Operators</a></span>.
</p><div class="note">
<a name="TP40014097-CH32-XID_665"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">At parse time, an expression made up of binary operators is represented as a flat list. This list is transformed into a tree by applying operator precedence For example, the expression <code class="code-voice">2 + 3 * 5</code> is initially understood as a flat list of five items, <code class="code-voice">2</code>, <code class="code-voice">+</code>, `` 3``, <code class="code-voice">*</code>, and <code class="code-voice">5</code>. This process transforms it into the tree (2 + (3 * 5)).
</p>
</aside>
</div><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a binary expression
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/binary-expression"></a>
<span class="syntax-def-name">
binary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/binary-operator" data-id="//apple_ref/swift/grammar/binary-operator">binary-operator</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/prefix-expression" data-id="//apple_ref/swift/grammar/prefix-expression">prefix-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_668"></a>
<span class="syntax-def-name">
binary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/assignment-operator" data-id="//apple_ref/swift/grammar/assignment-operator">assignment-operator</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/prefix-expression" data-id="//apple_ref/swift/grammar/prefix-expression">prefix-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_669"></a>
<span class="syntax-def-name">
binary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/conditional-operator" data-id="//apple_ref/swift/grammar/conditional-operator">conditional-operator</a></span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/prefix-expression" data-id="//apple_ref/swift/grammar/prefix-expression">prefix-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_670"></a>
<span class="syntax-def-name">
binary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="Types.html#//apple_ref/swift/grammar/type-casting-operator" data-id="//apple_ref/swift/grammar/type-casting-operator">type-casting-operator</a></span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/binary-expressions"></a>
<span class="syntax-def-name">
binary-expressions
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/binary-expression" data-id="//apple_ref/swift/grammar/binary-expression">binary-expression</a></span><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/binary-expressions" data-id="//apple_ref/swift/grammar/binary-expressions">binary-expressions</a></span><sub class="subscript">opt</sub></span>
</p>
</div>
</div>
<section class="section">
<a name="TP40014097-CH32-XID_672"></a>
<h3 class="section-name" tabindex="0">Assignment Operator</h3>
<p class="para">
The <em>assigment operator</em> sets a new value for a given expression. It has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><em class="variable-text">expression</em> = <em class="variable-text">value</em></pre></li>
</ul>
</div><p class="para">
The value of the <em>expression</em> is set to the value obtained by evaluating the <em>value</em>. If the <em>expression</em> is a tuple, the <em>value</em> must be a tuple with the same number of elements. (Nested tuples are allowed.) Assignment is performed from each part of the <em>value</em> to the corresponding part of the <em>expression</em>. For 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="vc">a</span>, <span class="kt">_</span>, (<span class="vc">b</span>, <span class="vc">c</span>)) = (<span class="s">"test"</span>, <span class="m">9.45</span>, (<span class="m">12</span>, <span class="m">3</span>))</code></li>
<li><code class="code-voice"><span class="c">// a is "test", b is 12, c is 3, and 9.45 is ignored</span></code></li>
</ul>
</div>
</section><p class="para">
The assignment operator does not return any value.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of an assignment operator
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/assignment-operator"></a>
<span class="syntax-def-name">
assignment-operator
</span>
<span class="arrow">
→
</span><code class="literal">=</code>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH32-XID_675"></a>
<h3 class="section-name" tabindex="0">Ternary Conditional Operator</h3>
<p class="para">
The <em>ternary conditional operator</em> evaluates to one of two given values based on the value of a condition. It has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><em class="variable-text">condition</em> ? <em class="variable-text">expression used if true</em> : <em class="variable-text">expression used if false</em></pre></li>
</ul>
</div><p class="para">
If the <em>condition</em> evaluates to <code class="code-voice">true</code>, the conditional operator evaluates the first expression and returns its value. Otherwise, it evaluates the second expression and returns its value. The unused expression is not evaluated.
</p><p class="para">
For an example that uses the ternary conditional operator, see <span class="x-name"><a href="Basic Operators.html#TP40014097-CH6-XID_84" data-id="TP40014097-CH6-XID_84">Ternary Conditional Operator</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a conditional operator
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/conditional-operator"></a>
<span class="syntax-def-name">
conditional-operator
</span>
<span class="arrow">
→
</span><code class="literal">?</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><code class="literal">:</code>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH32-XID_678"></a>
<h3 class="section-name" tabindex="0">Type-Casting Operators</h3>
<p class="para">
There are two type-casting operators, the <code class="code-voice">as</code> operator and the <code class="code-voice">is</code> operator. They have the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><em class="variable-text">expression</em> <span class="kt">as</span> <em class="variable-text">type</em></pre></li><li><pre class="code-voice"><em class="variable-text">expression</em> <span class="kt">as</span>? <em class="variable-text">type</em></pre></li><li><pre class="code-voice"><em class="variable-text">expression</em> <span class="kt">is</span> <em class="variable-text">type</em></pre></li>
</ul>
</div><p class="para">
The <code class="code-voice">as</code> operator performs a cast of the <em>expression</em> to the specified <em>type</em>. It behaves as follows:
</p><ul class="list-bullet">
<li class="item"><p class="para">
If conversion to the specified <em>type</em> is guaranteed to succeed, the value of the <em>expression</em> is returned as an instance of the specified <em>type</em>. An example is casting from a subclass to a superclass.
</p>
</li><li class="item"><p class="para">
If conversion to the specified <em>type</em> is guaranteed to fail, a compile-time error is raised.
</p>
</li><li class="item"><p class="para">
Otherwise, if it’s not known at compile time whether the conversion will succeed, the type of the cast expresion is an optional of the specified <em>type</em>. At runtime, if the cast succeeds, the value of <em>expression</em> is wrapped in an optional and returned; otherwise, the value returned is <code class="code-voice">nil</code>. An example is casting from a superclass to a subclass.
</p>
</li>
</ul><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">SomeSuperType</span> {}</code></li>
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">SomeType</span>: <span class="n"></span> {}</code></li>
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">SomeChildType</span>: <span class="n"></span> {}</code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">s</span> = <span class="vc">SomeType</span>()</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">x</span> = <span class="vc">s</span> <span class="kt">as</span> <span class="n"></span> <span class="c">// known to succeed; type is SomeSuperType</span></code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">y</span> = <span class="vc">s</span> <span class="kt">as</span> <span class="n"></span> <span class="c">// known to fail; compile-time error</span></code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">z</span> = <span class="vc">s</span> <span class="kt">as</span> <span class="n"></span> <span class="c">// might fail at runtime; type is SomeChildType?</span></code></li>
</ul>
</div>
</section><p class="para">
Specifying a type with <code class="code-voice">as</code> provides the same information to the compiler as a type annotation, as shown in the following 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">y1</span> = <span class="vc">x</span> <span class="kt">as</span> <span class="n"></span> <span class="c">// Type information from 'as'</span></code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">y2</span>: <span class="n"></span> = <span class="vc">x</span> <span class="c">// Type information from an annotation</span></code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">is</code> operator checks at runtime to see whether the <em>expression</em> is of the specified <em>type</em>. If so, it returns <code class="code-voice">true</code>; otherwise, it returns <code class="code-voice">false</code>.
</p><p class="para">
The check must not be known to be true or false at compile time. The following are invalid:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="s">"hello"</span> <span class="kt">is</span> <span class="n"></span></code></li>
<li><code class="code-voice"><span class="s">"hello"</span> <span class="kt">is</span> <span class="n"></span></code></li>
</ul>
</div>
</section><p class="para">
For more information about type casting and to see more examples that use the type-casting operators, see <span class="x-name"><a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443">Type Casting</a></span>.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a type-casting operator
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/type-casting-operator"></a>
<span class="syntax-def-name">
type-casting-operator
</span>
<span class="arrow">
→
</span><span class="alternative">
<code class="literal">is</code><span class="syntactic-cat"><a href="Types.html#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span>
</span><span class="alternative">
<code class="literal">as</code><span class="optional"><code class="literal">?</code><sub class="subscript">opt</sub></span><span class="syntactic-cat"><a href="Types.html#//apple_ref/swift/grammar/type" data-id="//apple_ref/swift/grammar/type">type</a></span>
</span>
</p>
</div>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH32-XID_681"></a>
<h3 class="section-name" tabindex="0">Primary Expressions</h3>
<p class="para">
<em>Primary expressions</em> are the most basic kind of expression. They can be used as expressions on their own, and they can be combined with other tokens to make prefix expressions, binary expressions, and postfix expressions.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a primary expression
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/primary-expression"></a>
<span class="syntax-def-name">
primary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="Lexical Structure.html#//apple_ref/swift/grammar/identifier" data-id="//apple_ref/swift/grammar/identifier">identifier</a></span><span class="optional"><span class="syntactic-cat"><a href="Generic Parameters and Arguments.html#//apple_ref/swift/grammar/generic-argument-clause" data-id="//apple_ref/swift/grammar/generic-argument-clause">generic-argument-clause</a></span><sub class="subscript">opt</sub></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_684"></a>
<span class="syntax-def-name">
primary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/literal-expression" data-id="//apple_ref/swift/grammar/literal-expression">literal-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_685"></a>
<span class="syntax-def-name">
primary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/self-expression" data-id="//apple_ref/swift/grammar/self-expression">self-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_686"></a>
<span class="syntax-def-name">
primary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/superclass-expression" data-id="//apple_ref/swift/grammar/superclass-expression">superclass-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_687"></a>
<span class="syntax-def-name">
primary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/closure-expression" data-id="//apple_ref/swift/grammar/closure-expression">closure-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_688"></a>
<span class="syntax-def-name">
primary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/parenthesized-expression" data-id="//apple_ref/swift/grammar/parenthesized-expression">parenthesized-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_689"></a>
<span class="syntax-def-name">
primary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/implicit-member-expression" data-id="//apple_ref/swift/grammar/implicit-member-expression">implicit-member-expression</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_690"></a>
<span class="syntax-def-name">
primary-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/wildcard-expression" data-id="//apple_ref/swift/grammar/wildcard-expression">wildcard-expression</a></span>
</p>
</div>
</div>
<section class="section">
<a name="TP40014097-CH32-XID_691"></a>
<h3 class="section-name" tabindex="0">Literal Expression</h3>
<p class="para">
A <em>literal expression</em> consists of either an ordinary literal (such as a string or a number), an array or dictionary literal, or one of the following special literals:
</p><div class="tableholder">
<table class="graybox" border="0" cellspacing="0" cellpadding="5">
<caption class="tablecaption"></caption>
<thead>
<tr>
<th scope="col" class="TableHeading_TableRow_TableCell"><p class="para">
Literal
</p></th>
<th scope="col" class="TableHeading_TableRow_TableCell"><p class="para">
Type
</p></th>
<th scope="col" class="TableHeading_TableRow_TableCell"><p class="para">
Value
</p></th>
</tr>
</thead>
<tbody>
<tr>
<td scope="row"><p class="para">
<code class="code-voice">__FILE__</code>
</p></td>
<td><p class="para">
String
</p></td>
<td><p class="para">
The name of the file in which it appears.
</p></td>
</tr>
<tr>
<td scope="row"><p class="para">
<code class="code-voice">__LINE__</code>
</p></td>
<td><p class="para">
Int
</p></td>
<td><p class="para">
The line number on which it appears.
</p></td>
</tr>
<tr>
<td scope="row"><p class="para">
<code class="code-voice">__COLUMN__</code>
</p></td>
<td><p class="para">
Int
</p></td>
<td><p class="para">
The column number in which it begins.
</p></td>
</tr>
<tr>
<td scope="row"><p class="para">
<code class="code-voice">__FUNCTION__</code>
</p></td>
<td><p class="para">
String
</p></td>
<td><p class="para">
The name of the declaration in which it appears.
</p></td>
</tr>
</tbody>
</table>
</div><p class="para">
Inside a function, the value of <code class="code-voice">__FUNCTION__</code> is the name of that function, inside a method it is the name of that method, inside a property getter or setter it is the name of that property, inside special members like <code class="code-voice">init</code> or <code class="code-voice">subscript</code> it is the name of that keyword, and at the top level of a file it is the name of the current module.
</p><p class="para">
An <em>array literal</em> is an ordered collection of values. It has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice">[<em class="variable-text">value 1</em>, <em class="variable-text">value 2</em>, <em class="variable-text">...</em>]</pre></li>
</ul>
</div><p class="para">
The last expression in the array can be followed by an optional comma. An empty array literal is written as an empty pair of brackets (<code class="code-voice">[]</code>). The value of an array literal has type <code class="code-voice">T[]</code>, where <code class="code-voice">T</code> is the type of the expressions inside it. If there are expressions of multiple types, <code class="code-voice">T</code> is their closest common supertype.
</p><p class="para">
A <em>dictionary literal</em> is an unordered collection of key-value pairs. It has the following form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice">[<em class="variable-text">key 1</em>: <em class="variable-text">value 1</em>, <em class="variable-text">key 2</em>: <em class="variable-text">value 2</em>, <em class="variable-text">...</em>]</pre></li>
</ul>
</div><p class="para">
The last expression in the dictionary can be followed by an optional comma. An empty dictionary literal is written as a colon inside a pair of brackets (<code class="code-voice">[:]</code>) to distinguish it from an empty array literal. The value of a dictionary literal has type <code class="code-voice">Dictionary<KeyType, ValueType></code>, where <code class="code-voice">KeyType</code> is the type of its key expressions and <code class="code-voice">ValueType</code> is the type of its value expressions. If there are expressions of multiple types, <code class="code-voice">KeyType</code> and <code class="code-voice">ValueType</code> are the closest common supertype for their respective values.
</p><div class="syntax-defs">
<p class="syntax-defs-name">
Grammar of a literal expression
</p><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/literal-expression"></a>
<span class="syntax-def-name">
literal-expression
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/literal" data-id="//apple_ref/swift/grammar/literal">literal</a></span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_694"></a>
<span class="syntax-def-name">
literal-expression
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/array-literal" data-id="//apple_ref/swift/grammar/array-literal">array-literal</a></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/dictionary-literal" data-id="//apple_ref/swift/grammar/dictionary-literal">dictionary-literal</a></span>
</span>
</p><p class="syntax-def">
<a name="TP40014097-CH32-XID_695"></a>
<span class="syntax-def-name">
literal-expression
</span>
<span class="arrow">
→
</span><span class="alternative">
<code class="literal">__FILE__</code>
</span><span class="alternative">
<code class="literal">__LINE__</code>
</span><span class="alternative">
<code class="literal">__COLUMN__</code>
</span><span class="alternative">
<code class="literal">__FUNCTION__</code>
</span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/array-literal"></a>
<span class="syntax-def-name">
array-literal
</span>
<span class="arrow">
→
</span><code class="literal">[</code><span class="optional"><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/array-literal-items" data-id="//apple_ref/swift/grammar/array-literal-items">array-literal-items</a></span><sub class="subscript">opt</sub></span><code class="literal">]</code>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/array-literal-items"></a>
<span class="syntax-def-name">
array-literal-items
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/array-literal-item" data-id="//apple_ref/swift/grammar/array-literal-item">array-literal-item</a></span><span class="optional"><code class="literal">,</code><sub class="subscript">opt</sub></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/array-literal-item" data-id="//apple_ref/swift/grammar/array-literal-item">array-literal-item</a></span><code class="literal">,</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/array-literal-items" data-id="//apple_ref/swift/grammar/array-literal-items">array-literal-items</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/array-literal-item"></a>
<span class="syntax-def-name">
array-literal-item
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span>
</p>
</div><div class="syntax-defs-group">
<p class="syntax-def">
<a name="//apple_ref/swift/grammar/dictionary-literal"></a>
<span class="syntax-def-name">
dictionary-literal
</span>
<span class="arrow">
→
</span><span class="alternative">
<code class="literal">[</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/dictionary-literal-items" data-id="//apple_ref/swift/grammar/dictionary-literal-items">dictionary-literal-items</a></span><code class="literal">]</code>
</span><span class="alternative">
<code class="literal">[</code><code class="literal">:</code><code class="literal">]</code>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/dictionary-literal-items"></a>
<span class="syntax-def-name">
dictionary-literal-items
</span>
<span class="arrow">
→
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/dictionary-literal-item" data-id="//apple_ref/swift/grammar/dictionary-literal-item">dictionary-literal-item</a></span><span class="optional"><code class="literal">,</code><sub class="subscript">opt</sub></span>
</span><span class="alternative">
<span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/dictionary-literal-item" data-id="//apple_ref/swift/grammar/dictionary-literal-item">dictionary-literal-item</a></span><code class="literal">,</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/dictionary-literal-items" data-id="//apple_ref/swift/grammar/dictionary-literal-items">dictionary-literal-items</a></span>
</span>
</p><p class="syntax-def">
<a name="//apple_ref/swift/grammar/dictionary-literal-item"></a>
<span class="syntax-def-name">
dictionary-literal-item
</span>
<span class="arrow">
→
</span><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span><code class="literal">:</code><span class="syntactic-cat"><a href="#//apple_ref/swift/grammar/expression" data-id="//apple_ref/swift/grammar/expression">expression</a></span>
</p>
</div>
</div>
</section>
<section class="section">
<a name="TP40014097-CH32-XID_702"></a>
<h3 class="section-name" tabindex="0">Self Expression</h3>
<p class="para">
The <code class="code-voice">self</code> expression is an explicit reference to the current type or instance of the type in which it occurs. It has the following forms:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice"><span class="kt">self</span></pre></li><li><pre class="code-voice"><span class="kt">self</span>.<em class="variable-text">member name</em></pre></li><li><pre class="code-voice"><span class="kt">self</span>[<em class="variable-text">subscript index</em>]</pre></li><li><pre class="code-voice"><span class="kt">self</span>(<em class="variable-text">initializer arguments</em>)</pre></li><li><pre class="code-voice"><span class="kt">self</span>.<span class="kt">init</span>(<em class="variable-text">initializer arguments</em>)</pre></li>
</ul>
</div><p class="para">
In an initializer, subscript, or instance method, <code class="code-voice">self</code> refers to the current instance of the type in which it occurs. In a static or class method, <code class="code-voice">self</code> refers to the current type in which it occurs.
</p><p class="para">
The <code class="code-voice">self</code> expression is used to specify scope when accessing members, providing disambiguation when there is another variable of the same name in scope, such as a function parameter. For example:
</p><section class="code-listing">