-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
2902 lines (2254 loc) · 160 KB
/
index.html
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><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Swift cheatsheet</title><style>:root {
--primary-color: #F05138;
--secondary-color: #F05138;
}
@media (prefers-color-scheme: dark) {
:root {
--primary-color: #F05138;
--secondary-color: #F05138;
}
}</style><link rel="stylesheet" href="./css/colors.css"><link rel="stylesheet" href="./css/style.css"><link rel="stylesheet" href="./css/code.css"><link rel="icon" href="./img/Swift.ico"><link rel="shortcut icon" href="./img/Swift/favicon.ico"><link rel="apple-touch-icon" href="./img/Swift/apple-touch-icon.png"></head><body><header><h1><b>Swift</b> cheatsheet</h1><p>A complete <a href="https://en.wikipedia.org/wiki/Swift_(programming_language)" target="_blank">Swift</a> programming language reference for beginners.</p></header><section><a id="getting-started"></a><h2><a href="#getting-started" class="anchor">Getting started</a></h2><div class="grid-321"><div class="card"><div class="flex"><a id="getting-started/hello-world"></a><h3><a href="#getting-started/hello-world" class="anchor">hello world</a></h3><pre class="swift"><code><span class="comment">// FILE: main.swift</span>
<span class="call">print</span>(<span class="string">"Hello, World! - 0"</span>)
<span class="comment">/**
Run Swift files using the command line:
$ swift main.swift
> Hello, World!
*/</span></code></pre><div id="tooltip-1_1" class="tooltip"><textarea id="snippet-code-1_1" class="original-code">// FILE: main.swift
print("Hello, World!")
/**
Run Swift files using the command line:
$ swift main.swift
> Hello, World!
*/</textarea><button id="copy-button-1_1" onclick="copySnippet('1_1')">Copy</button></div><p class="note">You can install Swift by following the guides on <a href="https://www.swift.org/getting-started/" target="_blank">swift.org</a>.</p></div></div><div class="card"><div class="flex"><a id="getting-started/comments"></a><h3><a href="#getting-started/comments" class="anchor">comments</a></h3><pre class="swift"><code><span class="comment">// this is just a single line comment
/**
multi-line comments are fun :)
__
/ _)
.-^^^-/ /
__/ /
<__.|_|-|_|
*/</span></code></pre><div id="tooltip-1_2" class="tooltip"><textarea id="snippet-code-1_2" class="original-code">// this is just a single line comment
/**
multi-line comments are fun :)
__
/ _)
.-^^^-/ /
__/ /
<__.|_|-|_|
*/</textarea><button id="copy-button-1_2" onclick="copySnippet('1_2')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/xcode/writing-symbol-documentation-in-your-source-files" target="_blank">Swift documentation</a></p></div></div><div class="card"><div class="flex"><a id="getting-started/print"></a><h3><a href="#getting-started/print" class="anchor">print</a></h3><pre class="swift"><code><span class="call">print</span>(<span class="string">"Hello, World!"</span>)
<span class="comment">// => Hello, World!\n</span>
<span class="call">print</span>(<span class="string">"Hello, World!"</span>, terminator: <span class="string">""</span>)
<span class="comment">// => Hello, World!</span>
<span class="call">print</span>(<span class="string">"foo"</span>, <span class="keyword">false</span>, <span class="number">42</span>, separator: <span class="string">", "</span>)
<span class="comment">// => foo, false, 42</span></code></pre><div id="tooltip-1_3" class="tooltip"><textarea id="snippet-code-1_3" class="original-code">print("Hello, World!")
// => Hello, World!\n
print("Hello, World!", terminator: "")
// => Hello, World!
print("foo", false, 42, separator: ", ")
// => foo, false, 42</textarea><button id="copy-button-1_3" onclick="copySnippet('1_3')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/print(_:separator:terminator:)" target="_blank">print</a></p></div></div><div class="card"><div class="flex"><a id="getting-started/constants-and-variables"></a><h3><a href="#getting-started/constants-and-variables" class="anchor">constants and variables</a></h3><pre class="swift"><code><span class="comment">// a constant</span>
<span class="keyword">let</span> name: <span class="type">String</span> = <span class="string">"John"</span>
<span class="call">print</span>(name) <span class="comment">// => John</span>
name = <span class="string">"Bob"</span> <span class="comment">// => ERROR
// Cannot assign to value:
// 'name' is a 'let' constant
// a variable</span>
<span class="keyword">var</span> age: <span class="type">Int</span> = <span class="number">42</span>
age = <span class="number">69</span>
<span class="call">print</span>(age) <span class="comment">// => 69</span></code></pre><div id="tooltip-1_4" class="tooltip"><textarea id="snippet-code-1_4" class="original-code">// a constant
let name: String = "John"
print(name) // => John
name = "Bob" // => ERROR
// Cannot assign to value:
// 'name' is a 'let' constant
// a variable
var age: Int = 42
age = 69
print(age) // => 69</textarea><button id="copy-button-1_4" onclick="copySnippet('1_4')">Copy</button></div></div></div><div class="card"><div class="flex"><a id="getting-started/characters-and-strings"></a><h3><a href="#getting-started/characters-and-strings" class="anchor">characters and strings</a></h3><pre class="swift"><code><span class="keyword">let</span> emoji: <span class="type">Character</span> = <span class="string">"😅"</span>
<span class="comment">// unicode representation of: ♥</span>
<span class="keyword">let</span> blackHeart: <span class="type">Character</span> = <span class="string">"\u{2665}"</span>
<span class="keyword">let</span> singleLine: <span class="type">String</span> = <span class="string">"Hello, World!"</span>
<span class="keyword">let</span> multiLine: <span class="type">String</span> = <span class="string">"""
Lorem ipsum
dolor sit amet
"""</span></code></pre><div id="tooltip-1_5" class="tooltip"><textarea id="snippet-code-1_5" class="original-code">let emoji: Character = "😅"
// unicode representation of: ♥
let blackHeart: Character = "\u{2665}"
let singleLine: String = "Hello, World!"
let multiLine: String = """
Lorem ipsum
dolor sit amet
"""</textarea><button id="copy-button-1_5" onclick="copySnippet('1_5')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/character" target="_blank">Character</a>, <a href="https://developer.apple.com/documentation/swift/string" target="_blank">String</a></p></div></div><div class="card"><div class="flex"><a id="getting-started/string-interpolation"></a><h3><a href="#getting-started/string-interpolation" class="anchor">string interpolation</a></h3><pre class="swift"><code><span class="keyword">let</span> name = <span class="string">"John"</span>
<span class="keyword">let</span> age = <span class="number">42</span>
<span class="keyword">let</span> end = <span class="string">"years old."</span>
<span class="comment">// String interpolation</span>
<span class="call">print</span>(<span class="string">"</span>\(name) <span class="string">is</span> \(age) \(end)<span class="string">."</span>)
<span class="comment">// => John is 42 years old.</span></code></pre><div id="tooltip-1_6" class="tooltip"><textarea id="snippet-code-1_6" class="original-code">let name = "John"
let age = 42
let end = "years old."
// String interpolation
print("\(name) is \(age) \(end).")
// => John is 42 years old.</textarea><button id="copy-button-1_6" onclick="copySnippet('1_6')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html" target="_blank">Strings and Characters</a></p></div></div><div class="card"><div class="flex"><a id="getting-started/basic-data-types"></a><h3><a href="#getting-started/basic-data-types" class="anchor">basic data types</a></h3><pre class="swift"><code><span class="keyword">let</span> b: <span class="type">Bool</span> = <span class="keyword">false
let</span> i: <span class="type">Int</span> = <span class="number">6</span>
<span class="keyword">let</span> f: <span class="type">Float</span> = <span class="number">4.20</span>
<span class="keyword">let</span> d: <span class="type">Double</span> = <span class="number">6.9</span>
<span class="keyword">let</span> s: <span class="type">String</span> = <span class="string">"Lorem ipsum"</span></code></pre><div id="tooltip-1_7" class="tooltip"><textarea id="snippet-code-1_7" class="original-code">let b: Bool = false
let i: Int = 6
let f: Float = 4.20
let d: Double = 6.9
let s: String = "Lorem ipsum"</textarea><button id="copy-button-1_7" onclick="copySnippet('1_7')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/bool" target="_blank">Bool</a>, <a href="https://developer.apple.com/documentation/swift/int" target="_blank">Int</a>, <a href="https://developer.apple.com/documentation/swift/float" target="_blank">Float</a>, <a href="https://developer.apple.com/documentation/swift/double" target="_blank">Double</a>, <a href="https://developer.apple.com/documentation/swift/string" target="_blank">String</a></p></div></div><div class="card"><div class="flex"><a id="getting-started/type-safety"></a><h3><a href="#getting-started/type-safety" class="anchor">type safety</a></h3><pre class="swift"><code><span class="keyword">var</span> name: <span class="type">String</span> = <span class="string">"John"</span>
name = <span class="number">69</span> <span class="comment">// => ERROR
// Cannot assign value of type
// 'Int' to type 'String'</span>
name = <span class="string">"Bob"</span> <span class="comment">// this will work</span></code></pre><div id="tooltip-1_8" class="tooltip"><textarea id="snippet-code-1_8" class="original-code">var name: String = "John"
name = 69 // => ERROR
// Cannot assign value of type
// 'Int' to type 'String'
name = "Bob" // this will work</textarea><button id="copy-button-1_8" onclick="copySnippet('1_8')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID322" target="_blank">Type Safety and Type Inference</a></p></div></div><div class="card"><div class="flex"><a id="getting-started/type-inference"></a><h3><a href="#getting-started/type-inference" class="anchor">type inference</a></h3><pre class="swift"><code><span class="keyword">let</span> bool = <span class="keyword">false
let</span> int = <span class="number">6</span>
<span class="keyword">let</span> double = <span class="number">6.9</span>
<span class="keyword">let</span> string = <span class="string">"Lorem ipsum"</span></code></pre><div id="tooltip-1_9" class="tooltip"><textarea id="snippet-code-1_9" class="original-code">let bool = false
let int = 6
let double = 6.9
let string = "Lorem ipsum"</textarea><button id="copy-button-1_9" onclick="copySnippet('1_9')">Copy</button></div><p class="note">Swift <a href="https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID322" target="_blank">infers</a> (automatically figures out) data types when assigning values to a constant or a variable.</p></div></div></div></section><section><a id="data-types"></a><h2><a href="#data-types" class="anchor">Data types</a></h2><div class="grid-321"><div class="card"><div class="flex"><a id="data-types/literals"></a><h3><a href="#data-types/literals" class="anchor">literals</a></h3><pre class="swift"><code><span class="comment">// bool</span>
<span class="keyword">let</span> value = <span class="keyword">false</span>
<span class="comment">// string</span>
<span class="keyword">let</span> foo = <span class="string">"foo"</span>
<span class="comment">// floating point</span>
<span class="keyword">let</span> pi = <span class="number">3.14</span>
<span class="comment">// integer</span>
<span class="keyword">let</span> decimal = <span class="number">42</span>
<span class="keyword">let</span> binary = 0b101010
<span class="keyword">let</span> octal = 0o52
<span class="keyword">let</span> hex = 0x2A
<span class="keyword">let</span> oneBillion = <span class="number">1_000_000_000</span>
<span class="comment">// other</span>
<span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]
<span class="keyword">let</span> dict = [<span class="string">"foo"</span>: <span class="number">42</span>, <span class="string">"bar"</span>: <span class="number">69</span>]
<span class="keyword">let</span> tuple = (<span class="number">404</span>, <span class="string">"Not found"</span>)</code></pre><div id="tooltip-2_1" class="tooltip"><textarea id="snippet-code-2_1" class="original-code">// bool
let value = false
// string
let foo = "foo"
// floating point
let pi = 3.14
// integer
let decimal = 42
let binary = 0b101010
let octal = 0o52
let hex = 0x2A
let oneBillion = 1_000_000_000
// other
let arr = [1, 2, 3]
let dict = ["foo": 42, "bar": 69]
let tuple = (404, "Not found")</textarea><button id="copy-button-2_1" onclick="copySnippet('2_1')">Copy</button></div></div></div><div class="card"><div class="flex"><a id="data-types/type-of"></a><h3><a href="#data-types/type-of" class="anchor">type(of:)</a></h3><pre class="swift"><code><span class="call">print</span>(<span class="call">type</span>(of: <span class="number">1</span>))
<span class="comment">// => Int</span>
<span class="call">print</span>(<span class="call">type</span>(of: <span class="string">"foo"</span>))
<span class="comment">// => String</span>
<span class="call">print</span>(<span class="call">type</span>(of: <span class="keyword">false</span>))
<span class="comment">// => Bool</span>
<span class="call">print</span>(<span class="call">type</span>(of: <span class="number">3.14</span>))
<span class="comment">// => Double</span></code></pre><div id="tooltip-2_2" class="tooltip"><textarea id="snippet-code-2_2" class="original-code">print(type(of: 1))
// => Int
print(type(of: "foo"))
// => String
print(type(of: false))
// => Bool
print(type(of: 3.14))
// => Double</textarea><button id="copy-button-2_2" onclick="copySnippet('2_2')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/type(of:)" target="_blank">type(of:)</a></p></div></div><div class="card"><div class="flex"><a id="data-types/optionals"></a><h3><a href="#data-types/optionals" class="anchor">optionals</a></h3><pre class="swift"><code><span class="keyword">var</span> success: <span class="type">Bool</span>? = <span class="keyword">false
var</span> responseCode: <span class="type">Int</span>?
<span class="keyword">var</span> error: <span class="type">Optional</span><<span class="type">String</span>> = <span class="keyword">nil</span>
print(responseCode) <span class="comment">// nil</span>
responseCode = <span class="number">404</span>
error = <span class="string">"Something went wrong"</span>
<span class="call">print</span>(success) <span class="comment">// => Optional(false)</span>
<span class="call">print</span>(responseCode) <span class="comment">// => Optional(404)</span></code></pre><div id="tooltip-2_3" class="tooltip"><textarea id="snippet-code-2_3" class="original-code">var success: Bool? = false
var responseCode: Int?
var error: Optional<String> = nil
print(responseCode) // nil
responseCode = 404
error = "Something went wrong"
print(success) // => Optional(false)
print(responseCode) // => Optional(404)</textarea><button id="copy-button-2_3" onclick="copySnippet('2_3')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/optional" target="_blank">Optional</a></p></div></div><div class="card"><div class="flex"><a id="data-types/arrays"></a><h3><a href="#data-types/arrays" class="anchor">arrays</a></h3><pre class="swift"><code><span class="comment">// an array of Int elements</span>
<span class="keyword">let</span> numbers: <span class="type">Array</span><<span class="type">Int</span>> = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]
<span class="comment">// an array of String elements</span>
<span class="keyword">let</span> names: [<span class="type">String</span>] = [<span class="string">"Joe"</span>, <span class="string">"Bob"</span>]
<span class="comment">// an empty array of Strings</span>
<span class="keyword">var</span> empty: [<span class="type">String</span>] = []
<span class="comment">// an array of Doubles</span>
<span class="keyword">var</span> mathConstants = [<span class="number">3.14</span>, <span class="number">2.71</span>, <span class="number">0.57</span>]
<span class="comment">// array of Any types</span>
<span class="keyword">var</span> array: [<span class="type">Any</span>] = [<span class="number">1</span>, <span class="string">"foo"</span>, <span class="keyword">false</span>, <span class="number">3.14</span>]</code></pre><div id="tooltip-2_4" class="tooltip"><textarea id="snippet-code-2_4" class="original-code">// an array of Int elements
let numbers: Array<Int> = [1, 2, 3, 4, 5]
// an array of String elements
let names: [String] = ["Joe", "Bob"]
// an empty array of Strings
var empty: [String] = []
// an array of Doubles
var mathConstants = [3.14, 2.71, 0.57]
// array of Any types
var array: [Any] = [1, "foo", false, 3.14]</textarea><button id="copy-button-2_4" onclick="copySnippet('2_4')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/array" target="_blank">Array</a></p></div></div><div class="card"><div class="flex"><a id="data-types/sets"></a><h3><a href="#data-types/sets" class="anchor">sets</a></h3><pre class="swift"><code><span class="comment">// a set of unique Int values</span>
<span class="keyword">let</span> values: <span class="type">Set</span><<span class="type">Int</span>> = [<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">5</span>]
<span class="call">print</span>(values)
<span class="comment">// => [3, 2, 5, 1] (or different)
// a set of String values</span>
<span class="keyword">let</span> names: <span class="type">Set</span> = [<span class="string">"John"</span>, <span class="string">"Bob"</span>]
<span class="comment">// an empty set of Strings</span>
<span class="keyword">var</span> empty: <span class="type">Set</span><<span class="type">String</span>> = []</code></pre><div id="tooltip-2_5" class="tooltip"><textarea id="snippet-code-2_5" class="original-code">// a set of unique Int values
let values: Set<Int> = [1, 1, 2, 3, 3, 5]
print(values)
// => [3, 2, 5, 1] (or different)
// a set of String values
let names: Set = ["John", "Bob"]
// an empty set of Strings
var empty: Set<String> = []</textarea><button id="copy-button-2_5" onclick="copySnippet('2_5')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/set" target="_blank">Set</a></p></div></div><div class="card"><div class="flex"><a id="data-types/dictionaries"></a><h3><a href="#data-types/dictionaries" class="anchor">dictionaries</a></h3><pre class="swift"><code><span class="keyword">var</span> responses: <span class="type">Dictionary</span><<span class="type">Int</span>, <span class="type">String</span>> = [
<span class="number">200</span>: <span class="string">"Ok"</span>,
<span class="number">404</span>: <span class="string">"Not found"</span>,
<span class="number">500</span>: <span class="string">"Internal server error"</span>,
]
<span class="keyword">let</span> scores: [<span class="type">String</span>: <span class="type">Double</span>] = [
<span class="string">"John"</span>: <span class="number">42.1</span>,
<span class="string">"Bob"</span>: <span class="number">31.6</span>,
]
<span class="keyword">var</span> empty: [<span class="type">String</span>: <span class="type">String</span>] = [:]</code></pre><div id="tooltip-2_6" class="tooltip"><textarea id="snippet-code-2_6" class="original-code">var responses: Dictionary<Int, String> = [
200: "Ok",
404: "Not found",
500: "Internal server error",
]
let scores: [String: Double] = [
"John": 42.1,
"Bob": 31.6,
]
var empty: [String: String] = [:]</textarea><button id="copy-button-2_6" onclick="copySnippet('2_6')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/dictionary" target="_blank">Dictionary</a></p></div></div><div class="card"><div class="flex"><a id="data-types/tuples"></a><h3><a href="#data-types/tuples" class="anchor">tuples</a></h3><pre class="swift"><code><span class="keyword">var</span> constants: (<span class="type">Double</span>, <span class="type">Double</span>, <span class="type">Double</span>) = (
<span class="number">3.14159</span>,
<span class="number">2.71828</span>,
<span class="number">0.57721</span>
)
<span class="call">print</span>(constants.<span class="number">0</span>, constants.<span class="number">1</span>, constant.<span class="number">2</span>)
<span class="comment">// => 3.14159 2.71828 0.57721</span>
constants.<span class="number">0</span> = <span class="number">4.20</span>
<span class="call">print</span>(constants.<span class="number">0</span>) <span class="comment">// => 4.20</span>
<span class="keyword">var</span> result = (
code: <span class="number">404</span>,
error: <span class="string">"Not found"</span>
)
<span class="call">print</span>(result.<span class="property">error</span>) <span class="comment">// Not found</span>
result.<span class="property">code</span> = <span class="number">200</span></code></pre><div id="tooltip-2_7" class="tooltip"><textarea id="snippet-code-2_7" class="original-code">var constants: (Double, Double, Double) = (
3.14159,
2.71828,
0.57721
)
print(constants.0, constants.1, constant.2)
// => 3.14159 2.71828 0.57721
constants.0 = 4.20
print(constants.0) // => 4.20
var result = (
code: 404,
error: "Not found"
)
print(result.error) // Not found
result.code = 200</textarea><button id="copy-button-2_7" onclick="copySnippet('2_7')">Copy</button></div></div></div><div class="card"><div class="flex"><a id="data-types/signed-integers"></a><h3><a href="#data-types/signed-integers" class="anchor">signed integers</a></h3><pre class="swift"><code><span class="keyword">let</span> i: <span class="type">Int</span> <span class="comment">// 32 or 64 bit</span>
<span class="keyword">let</span> i1: <span class="type">Int8</span> <span class="comment">// 8 bit</span>
<span class="keyword">let</span> i2: <span class="type">Int16</span> <span class="comment">// 16 bit</span>
<span class="keyword">let</span> i3: <span class="type">Int32</span> <span class="comment">// 32 bit</span>
<span class="keyword">let</span> i4: <span class="type">Int64</span> <span class="comment">// 64 bit
// you can get min and max values</span>
<span class="call">print</span>(<span class="type">Int8</span>.<span class="property">min</span>) <span class="comment">// => -128</span>
<span class="call">print</span>(<span class="type">Int8</span>.<span class="property">max</span>) <span class="comment">// => 127</span></code></pre><div id="tooltip-2_8" class="tooltip"><textarea id="snippet-code-2_8" class="original-code">let i: Int // 32 or 64 bit
let i1: Int8 // 8 bit
let i2: Int16 // 16 bit
let i3: Int32 // 32 bit
let i4: Int64 // 64 bit
// you can get min and max values
print(Int8.min) // => -128
print(Int8.max) // => 127</textarea><button id="copy-button-2_8" onclick="copySnippet('2_8')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/int" target="_blank">Int</a>, <a href="https://developer.apple.com/documentation/swift/int8" target="_blank">Int8</a>, <a href="https://developer.apple.com/documentation/swift/int16" target="_blank">Int16</a>, <a href="https://developer.apple.com/documentation/swift/int32" target="_blank">Int32</a>, <a href="https://developer.apple.com/documentation/swift/int64" target="_blank">Int64</a></p></div></div><div class="card"><div class="flex"><a id="data-types/unsigned-integers"></a><h3><a href="#data-types/unsigned-integers" class="anchor">unsigned integers</a></h3><pre class="swift"><code><span class="keyword">let</span> u: <span class="type">UInt</span> <span class="comment">// 32 or 64 bit</span>
<span class="keyword">let</span> u1: <span class="type">UInt8</span> <span class="comment">// 8 bit</span>
<span class="keyword">let</span> u2: <span class="type">UInt16</span> <span class="comment">// 16 bit</span>
<span class="keyword">let</span> u3: <span class="type">UInt32</span> <span class="comment">// 32 bit</span>
<span class="keyword">let</span> u4: <span class="type">UInt64</span> <span class="comment">// 64 bit</span>
<span class="call">print</span>(<span class="type">UInt8</span>.<span class="property">min</span>) <span class="comment">// => 0</span>
<span class="call">print</span>(<span class="type">UInt8</span>.<span class="property">max</span>) <span class="comment">// => 255</span></code></pre><div id="tooltip-2_9" class="tooltip"><textarea id="snippet-code-2_9" class="original-code">let u: UInt // 32 or 64 bit
let u1: UInt8 // 8 bit
let u2: UInt16 // 16 bit
let u3: UInt32 // 32 bit
let u4: UInt64 // 64 bit
print(UInt8.min) // => 0
print(UInt8.max) // => 255</textarea><button id="copy-button-2_9" onclick="copySnippet('2_9')">Copy</button></div><p class="references">References: <a href="https://developer.apple.com/documentation/swift/uint" target="_blank">UInt</a>, <a href="https://developer.apple.com/documentation/swift/uint8" target="_blank">UInt8</a>, <a href="https://developer.apple.com/documentation/swift/uint16" target="_blank">UInt16</a>, <a href="https://developer.apple.com/documentation/swift/uint32" target="_blank">UInt32</a>, <a href="https://developer.apple.com/documentation/swift/uint64" target="_blank">UInt64</a></p></div></div></div></section><section><a id="operators"></a><h2><a href="#operators" class="anchor">Operators</a></h2><div class="grid-321"><div class="card"><div class="flex"><a id="operators/assignment"></a><h3><a href="#operators/assignment" class="anchor">assignment</a></h3><pre class="swift"><code><span class="keyword">let</span> a = <span class="string">"foo"</span>
<span class="keyword">var</span> b = <span class="number">42</span>
<span class="keyword">let</span> c = <span class="number">69</span>
b = c
<span class="call">print</span>(b) <span class="comment">// => 69</span></code></pre><div id="tooltip-3_1" class="tooltip"><textarea id="snippet-code-3_1" class="original-code">let a = "foo"
var b = 42
let c = 69
b = c
print(b) // => 69</textarea><button id="copy-button-3_1" onclick="copySnippet('3_1')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID62" target="_blank">Assignment Operator</a></p></div></div><div class="card"><div class="flex"><a id="operators/arithmetic"></a><h3><a href="#operators/arithmetic" class="anchor">arithmetic</a></h3><pre class="swift"><code><span class="keyword">let</span> x = <span class="number">5</span>
<span class="keyword">let</span> y = <span class="number">5</span>
<span class="keyword">var</span> z = <span class="number">0</span>
z = x + y <span class="comment">// addition</span>
<span class="call">print</span>(z) <span class="comment">// => 10</span>
z = x - y <span class="comment">// subtraction</span>
<span class="call">print</span>(z) <span class="comment">// => 0</span>
z = x * y <span class="comment">// multiplication</span>
<span class="call">print</span>(z) <span class="comment">// => 25</span>
z = x / y <span class="comment">// division</span>
<span class="call">print</span>(z) <span class="comment">// => 1</span></code></pre><div id="tooltip-3_2" class="tooltip"><textarea id="snippet-code-3_2" class="original-code">let x = 5
let y = 5
var z = 0
z = x + y // addition
print(z) // => 10
z = x - y // subtraction
print(z) // => 0
z = x * y // multiplication
print(z) // => 25
z = x / y // division
print(z) // => 1</textarea><button id="copy-button-3_2" onclick="copySnippet('3_2')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID63" target="_blank">Arithmetic Operators</a></p></div></div><div class="card"><div class="flex"><a id="operators/compound-assignment"></a><h3><a href="#operators/compound-assignment" class="anchor">compound assignment</a></h3><pre class="swift"><code><span class="keyword">var</span> x = <span class="number">10</span>
x += <span class="number">2</span> <span class="comment">// addition</span>
<span class="call">print</span>(x) <span class="comment">// => 12</span>
x -= <span class="number">2</span> <span class="comment">// subtraction</span>
<span class="call">print</span>(x) <span class="comment">// => 10</span>
x *= <span class="number">2</span> <span class="comment">// multiplication</span>
<span class="call">print</span>(x) <span class="comment">// => 20</span>
x /= <span class="number">2</span> <span class="comment">// division</span>
<span class="call">print</span>(x) <span class="comment">// => 10</span></code></pre><div id="tooltip-3_3" class="tooltip"><textarea id="snippet-code-3_3" class="original-code">var x = 10
x += 2 // addition
print(x) // => 12
x -= 2 // subtraction
print(x) // => 10
x *= 2 // multiplication
print(x) // => 20
x /= 2 // division
print(x) // => 10</textarea><button id="copy-button-3_3" onclick="copySnippet('3_3')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID69" target="_blank">Compound Assignment Operators</a></p></div></div><div class="card"><div class="flex"><a id="operators/comparison"></a><h3><a href="#operators/comparison" class="anchor">comparison</a></h3><pre class="swift"><code><span class="keyword">let</span> x = <span class="number">6</span>
<span class="keyword">let</span> y = <span class="number">9</span>
<span class="keyword">var</span> z = <span class="keyword">false</span>
z = x == y <span class="comment">// equal</span>
<span class="call">print</span>(z) <span class="comment">// false</span>
z = x != y <span class="comment">// not equal</span>
<span class="call">print</span>(z) <span class="comment">// true</span>
z = x > y <span class="comment">// greater than</span>
<span class="call">print</span>(z) <span class="comment">// false</span>
z = x >= y <span class="comment">// greater than or equal</span>
<span class="call">print</span>(z) <span class="comment">// false</span>
z = x < y <span class="comment">// less than</span>
<span class="call">print</span>(z) <span class="comment">// true</span>
z = x <= y <span class="comment">// less than or equal</span>
<span class="call">print</span>(z) <span class="comment">// true</span></code></pre><div id="tooltip-3_4" class="tooltip"><textarea id="snippet-code-3_4" class="original-code">let x = 6
let y = 9
var z = false
z = x == y // equal
print(z) // false
z = x != y // not equal
print(z) // true
z = x > y // greater than
print(z) // false
z = x >= y // greater than or equal
print(z) // false
z = x < y // less than
print(z) // true
z = x <= y // less than or equal
print(z) // true</textarea><button id="copy-button-3_4" onclick="copySnippet('3_4')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID70" target="_blank">Comparison Operators</a></p></div></div><div class="card"><div class="flex"><a id="operators/ternary-conditional"></a><h3><a href="#operators/ternary-conditional" class="anchor">ternary conditional</a></h3><pre class="swift"><code><span class="keyword">let</span> x = <span class="number">12</span>
<span class="keyword">let</span> result1 = x > <span class="number">10</span> ? <span class="string">"win"</span> : <span class="string">"lose"</span>
<span class="call">print</span>(<span class="string">"You "</span> + result2)
<span class="comment">// => You win</span>
<span class="keyword">let</span> y = <span class="number">7</span>
<span class="keyword">let</span> result2 = y > <span class="number">10</span> ? <span class="string">"win"</span> : <span class="string">"lose"</span>
<span class="call">print</span>(<span class="string">"You "</span> + result2)
<span class="comment">// => You lose</span></code></pre><div id="tooltip-3_5" class="tooltip"><textarea id="snippet-code-3_5" class="original-code">let x = 12
let result1 = x > 10 ? "win" : "lose"
print("You " + result2)
// => You win
let y = 7
let result2 = y > 10 ? "win" : "lose"
print("You " + result2)
// => You lose</textarea><button id="copy-button-3_5" onclick="copySnippet('3_5')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID71" target="_blank">Ternary Conditional Operator</a></p></div></div><div class="card"><div class="flex"><a id="operators/nil-coalescing"></a><h3><a href="#operators/nil-coalescing" class="anchor">nil-coalescing</a></h3><pre class="swift"><code><span class="keyword">let</span> error1: <span class="type">String</span>? = <span class="string">"Server error"</span>
<span class="keyword">let</span> message1 = error1 ?? <span class="string">"Unknown error"</span>
<span class="call">print</span>(message1)
<span class="comment">// => Server error</span>
<span class="keyword">let</span> error2: <span class="type">String</span>? = <span class="keyword">nil
let</span> message2 = error2 ?? <span class="string">"Unknown error"</span>
<span class="call">print</span>(message2)
<span class="comment">// => Unknown error</span></code></pre><div id="tooltip-3_6" class="tooltip"><textarea id="snippet-code-3_6" class="original-code">let error1: String? = "Server error"
let message1 = error1 ?? "Unknown error"
print(message1)
// => Server error
let error2: String? = nil
let message2 = error2 ?? "Unknown error"
print(message2)
// => Unknown error</textarea><button id="copy-button-3_6" onclick="copySnippet('3_6')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID72" target="_blank">Nil-Coalescing Operator</a></p></div></div><div class="card"><div class="flex"><a id="operators/range"></a><h3><a href="#operators/range" class="anchor">range</a></h3><pre class="swift"><code><span class="comment">// closed range</span>
<span class="keyword">let</span> a = <span class="number">1</span>...<span class="number">5</span>
<span class="comment">// half open range</span>
<span class="keyword">let</span> b = <span class="number">0</span>..<<span class="number">9</span>
<span class="comment">// one-sided ranges</span>
<span class="keyword">let</span> c = <span class="number">2</span>...
<span class="keyword">let</span> d = ...<span class="number">2</span>
<span class="keyword">let</span> e = ..<<span class="number">2</span></code></pre><div id="tooltip-3_7" class="tooltip"><textarea id="snippet-code-3_7" class="original-code">// closed range
let a = 1...5
// half open range
let b = 0..<9
// one-sided ranges
let c = 2...
let d = ...2
let e = ..<2</textarea><button id="copy-button-3_7" onclick="copySnippet('3_7')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID73" target="_blank">Range Operators</a></p></div></div><div class="card"><div class="flex"><a id="operators/logical"></a><h3><a href="#operators/logical" class="anchor">logical</a></h3><pre class="swift"><code><span class="keyword">let</span> x = <span class="keyword">true
let</span> y = <span class="keyword">false</span>
<span class="comment">// NOT</span>
<span class="keyword">let</span> r1 = !x
<span class="call">print</span>(r1) <span class="comment">// => false
// AND</span>
<span class="keyword">let</span> r2 = x && y
<span class="call">print</span>(r2) <span class="comment">// => false
// OR</span>
<span class="keyword">let</span> r3 = x || y
<span class="call">print</span>(r3) <span class="comment">// => true</span></code></pre><div id="tooltip-3_8" class="tooltip"><textarea id="snippet-code-3_8" class="original-code">let x = true
let y = false
// NOT
let r1 = !x
print(r1) // => false
// AND
let r2 = x && y
print(r2) // => false
// OR
let r3 = x || y
print(r3) // => true</textarea><button id="copy-button-3_8" onclick="copySnippet('3_8')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html#ID76" target="_blank">Logical Operators</a></p></div></div><div class="card"><div class="flex"><a id="operators/other"></a><h3><a href="#operators/other" class="anchor">other</a></h3><pre class="swift"><code><span class="comment">// String concatenation</span>
<span class="call">print</span>(<span class="string">"foo"</span> + <span class="string">" "</span> + <span class="string">"bar"</span>) <span class="comment">// => foo bar
// remainder</span>
<span class="call">print</span>(<span class="number">10</span> % <span class="number">6</span>) <span class="comment">// => 4
// unary plus</span>
<span class="call">print</span>(+<span class="number">42</span>) <span class="comment">// => 42
// unary minus</span>
<span class="call">print</span>(-<span class="number">69</span>) <span class="comment">// => -69</span></code></pre><div id="tooltip-3_9" class="tooltip"><textarea id="snippet-code-3_9" class="original-code">// String concatenation
print("foo" + " " + "bar") // => foo bar
// remainder
print(10 % 6) // => 4
// unary plus
print(+42) // => 42
// unary minus
print(-69) // => -69</textarea><button id="copy-button-3_9" onclick="copySnippet('3_9')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html" target="_blank">Operators</a></p></div></div></div></section><section><a id="control-flow"></a><h2><a href="#control-flow" class="anchor">Control flow</a></h2><div class="grid-321"><div class="card"><div class="flex"><a id="control-flow/if"></a><h3><a href="#control-flow/if" class="anchor">if</a></h3><pre class="swift"><code><span class="keyword">var</span> isItRaining = <span class="keyword">true
if</span> isItRaining {
<span class="call">print</span>(<span class="string">"Take an umbrella."</span>)
}
<span class="comment">// => Take an umbrella.</span></code></pre><div id="tooltip-4_1" class="tooltip"><textarea id="snippet-code-4_1" class="original-code">var isItRaining = true
if isItRaining {
print("Take an umbrella.")
}
// => Take an umbrella.</textarea><button id="copy-button-4_1" onclick="copySnippet('4_1')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/ReferenceManual/Statements.html#ID435" target="_blank">If Statement</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/if-else"></a><h3><a href="#control-flow/if-else" class="anchor">if-else</a></h3><pre class="swift"><code><span class="keyword">var</span> score = <span class="number">4</span>
<span class="keyword">if</span> score > <span class="number">5</span> {
<span class="call">print</span>(<span class="string">"Great job."</span>)
}
<span class="keyword">else</span> {
<span class="call">print</span>(<span class="string">"Nice try."</span>)
}
<span class="comment">// => Nice try.</span></code></pre><div id="tooltip-4_2" class="tooltip"><textarea id="snippet-code-4_2" class="original-code">var score = 4
if score > 5 {
print("Great job.")
}
else {
print("Nice try.")
}
// => Nice try.</textarea><button id="copy-button-4_2" onclick="copySnippet('4_2')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/ReferenceManual/Statements.html#ID435" target="_blank">If Statement</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/if-elseif-else"></a><h3><a href="#control-flow/if-elseif-else" class="anchor">if-elseif-else</a></h3><pre class="swift"><code><span class="keyword">var</span> score = <span class="number">3</span>
<span class="keyword">if</span> score > <span class="number">5</span> {
<span class="call">print</span>(<span class="string">"Great job."</span>)
}
<span class="keyword">else if</span> score > <span class="number">2</span> {
<span class="call">print</span>(<span class="string">"Not bad."</span>)
}
<span class="keyword">else</span> {
<span class="call">print</span>(<span class="string">"Nice try."</span>)
}
<span class="comment">// => Not bad.</span></code></pre><div id="tooltip-4_3" class="tooltip"><textarea id="snippet-code-4_3" class="original-code">var score = 3
if score > 5 {
print("Great job.")
}
else if score > 2 {
print("Not bad.")
}
else {
print("Nice try.")
}
// => Not bad.</textarea><button id="copy-button-4_3" onclick="copySnippet('4_3')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/ReferenceManual/Statements.html#ID435" target="_blank">If Statement</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/guard"></a><h3><a href="#control-flow/guard" class="anchor">guard</a></h3><pre class="swift"><code><span class="keyword">var</span> score = <span class="number">3</span>
<span class="keyword">guard</span> score > <span class="number">1</span> <span class="keyword">else</span> {
<span class="call">fatalError</span>(<span class="string">"You've failed."</span>)
}
<span class="call">print</span>(<span class="string">"You've passed."</span>)
<span class="comment">// => You've passed.</span></code></pre><div id="tooltip-4_4" class="tooltip"><textarea id="snippet-code-4_4" class="original-code">var score = 3
guard score > 1 else {
fatalError("You've failed.")
}
print("You've passed.")
// => You've passed.</textarea><button id="copy-button-4_4" onclick="copySnippet('4_4')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/ReferenceManual/Statements.html#ID524" target="_blank">Guard Statement</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/switch"></a><h3><a href="#control-flow/switch" class="anchor">switch</a></h3><pre class="swift"><code><span class="keyword">var</span> statusCode = <span class="number">404</span>
<span class="keyword">switch</span> statusCode {
<span class="keyword">case</span> <span class="number">200</span>:
<span class="call">print</span>(<span class="string">"Ok"</span>)
<span class="keyword">case</span> <span class="number">404</span>:
<span class="call">print</span>(<span class="string">"Not found"</span>)
<span class="keyword">default</span>:
<span class="call">print</span>(statusCode)
}
<span class="comment">// => Not found</span></code></pre><div id="tooltip-4_5" class="tooltip"><textarea id="snippet-code-4_5" class="original-code">var statusCode = 404
switch statusCode {
case 200:
print("Ok")
case 404:
print("Not found")
default:
print(statusCode)
}
// => Not found</textarea><button id="copy-button-4_5" onclick="copySnippet('4_5')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/ReferenceManual/Statements.html#ID436" target="_blank">Switch Statement</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/optional-binding"></a><h3><a href="#control-flow/optional-binding" class="anchor">optional binding</a></h3><pre class="swift"><code><span class="keyword">var</span> possibleName: <span class="type">String</span>? = <span class="string">"John"</span>
<span class="keyword">if let</span> name = possibleName {
<span class="call">print</span>(name) <span class="comment">// => John</span>
}
<span class="keyword">if let</span> possibleName {
<span class="call">print</span>(possibleName) <span class="comment">// => John</span>
}
<span class="keyword">guard let</span> name = possibleName <span class="keyword">else</span> {
<span class="call">fatalError</span>(<span class="string">"Missing name"</span>)
}
<span class="call">print</span>(<span class="string">"Hello,</span> \(name)<span class="string">!"</span>)
<span class="comment">// => Hello, John!</span></code></pre><div id="tooltip-4_6" class="tooltip"><textarea id="snippet-code-4_6" class="original-code">var possibleName: String? = "John"
if let name = possibleName {
print(name) // => John
}
if let possibleName {
print(possibleName) // => John
}
guard let name = possibleName else {
fatalError("Missing name")
}
print("Hello, \(name)!")
// => Hello, John!</textarea><button id="copy-button-4_6" onclick="copySnippet('4_6')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID333" target="_blank">Optional binding</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/for-in"></a><h3><a href="#control-flow/for-in" class="anchor">for-in</a></h3><pre class="swift"><code><span class="keyword">let</span> names = [<span class="string">"John"</span>, <span class="string">"Bob"</span>]
<span class="keyword">for</span> name <span class="keyword">in</span> names {
<span class="call">print</span>(<span class="string">"Hello,</span> \(name)<span class="string">!"</span>)
}
<span class="comment">// => Hello, John!
// => Hello, Bob!</span>
<span class="keyword">let</span> scores = [
<span class="string">"John"</span>: <span class="number">8</span>,
<span class="string">"Bob"</span>: <span class="number">7</span>,
]
<span class="keyword">for</span> (name, score) <span class="keyword">in</span> scores {
<span class="call">print</span>(<span class="string">"</span>\(name)<span class="string">'s score:</span> \(score)<span class="string">"</span>)
}
<span class="comment">// => John's score: 8
// => Bob's score: 7</span></code></pre><div id="tooltip-4_7" class="tooltip"><textarea id="snippet-code-4_7" class="original-code">let names = ["John", "Bob"]
for name in names {
print("Hello, \(name)!")
}
// => Hello, John!
// => Hello, Bob!
let scores = [
"John": 8,
"Bob": 7,
]
for (name, score) in scores {
print("\(name)'s score: \(score)")
}
// => John's score: 8
// => Bob's score: 7</textarea><button id="copy-button-4_7" onclick="copySnippet('4_7')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID121" target="_blank">For-In Loops</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/while"></a><h3><a href="#control-flow/while" class="anchor">while</a></h3><pre class="swift"><code><span class="keyword">var</span> i = <span class="number">0</span>
<span class="keyword">while</span> i < <span class="number">3</span> {
<span class="call">print</span>(<span class="string">"</span>\(i + <span class="number">1</span>)<span class="string">."</span>)
i += <span class="number">1</span>
}
<span class="comment">// => 1.
// => 2.
// => 3.</span></code></pre><div id="tooltip-4_8" class="tooltip"><textarea id="snippet-code-4_8" class="original-code">var i = 0
while i < 3 {
print("\(i + 1).")
i += 1
}
// => 1.
// => 2.
// => 3.</textarea><button id="copy-button-4_8" onclick="copySnippet('4_8')">Copy</button></div><p class="note">The while block won't be executed if the condition is not true.</p><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID124" target="_blank">While Loops</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/repeat-while"></a><h3><a href="#control-flow/repeat-while" class="anchor">repeat-while</a></h3><pre class="swift"><code><span class="keyword">var</span> i = <span class="number">0</span>
<span class="keyword">repeat</span> {
<span class="call">print</span>(<span class="string">"</span>\(i + <span class="number">1</span>)<span class="string">."</span>)
i += <span class="number">1</span>
}
<span class="keyword">while</span> i < <span class="number">3</span>
<span class="comment">// => 1.
// => 2.
// => 3.</span></code></pre><div id="tooltip-4_9" class="tooltip"><textarea id="snippet-code-4_9" class="original-code">var i = 0
repeat {
print("\(i + 1).")
i += 1
}
while i < 3
// => 1.
// => 2.
// => 3.</textarea><button id="copy-button-4_9" onclick="copySnippet('4_9')">Copy</button></div><p class="note">The repeat block will be executed at least 1 time, if the while condition is true, the loop keeps going.</p><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID126" target="_blank">Repeat-While Loops</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/break"></a><h3><a href="#control-flow/break" class="anchor">break</a></h3><pre class="swift"><code><span class="keyword">for</span> i <span class="keyword">in</span> <span class="number">1</span>...<span class="number">5</span> {
<span class="keyword">if</span> i == <span class="number">4</span> {
<span class="keyword">break</span>
}
<span class="call">print</span>(i)
}
<span class="comment">// => 1
// => 2
// => 3</span></code></pre><div id="tooltip-4_10" class="tooltip"><textarea id="snippet-code-4_10" class="original-code">for i in 1...5 {
if i == 4 {
break
}
print(i)
}
// => 1
// => 2
// => 3</textarea><button id="copy-button-4_10" onclick="copySnippet('4_10')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID137" target="_blank">Break</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/continue"></a><h3><a href="#control-flow/continue" class="anchor">continue</a></h3><pre class="swift"><code><span class="keyword">for</span> i <span class="keyword">in</span> <span class="number">1</span>...<span class="number">5</span> {
<span class="keyword">if</span> i == <span class="number">4</span> {
<span class="keyword">continue</span>
}
<span class="call">print</span>(i)
}
<span class="comment">// => 1
// => 2
// => 3
// => 5</span></code></pre><div id="tooltip-4_11" class="tooltip"><textarea id="snippet-code-4_11" class="original-code">for i in 1...5 {
if i == 4 {
continue
}
print(i)
}
// => 1
// => 2
// => 3
// => 5</textarea><button id="copy-button-4_11" onclick="copySnippet('4_11')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID136" target="_blank">Continue</a></p></div></div><div class="card"><div class="flex"><a id="control-flow/fallthrough"></a><h3><a href="#control-flow/fallthrough" class="anchor">fallthrough</a></h3><pre class="swift"><code><span class="keyword">let</span> number = <span class="number">5</span>
<span class="keyword">var</span> description = <span class="string">"The number is"</span>
<span class="keyword">switch</span> number {
<span class="keyword">case</span> <span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>:
description += <span class="string">" a prime, and also"</span>
<span class="keyword">fallthrough
default</span>:
description += <span class="string">" an integer."</span>
}
<span class="call">print</span>(description)
<span class="comment">// => The number is a prime,
// and also an integer.</span></code></pre><div id="tooltip-4_12" class="tooltip"><textarea id="snippet-code-4_12" class="original-code">let number = 5
var description = "The number is"
switch number {
case 2, 3, 5, 7:
description += " a prime, and also"
fallthrough
default:
description += " an integer."
}
print(description)
// => The number is a prime,
// and also an integer.</textarea><button id="copy-button-4_12" onclick="copySnippet('4_12')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID140" target="_blank">Fallthrough</a></p></div></div></div></section><section><a id="functions"></a><h2><a href="#functions" class="anchor">Functions</a></h2><div class="grid-321"><div class="card"><div class="flex"><a id="functions/declaration"></a><h3><a href="#functions/declaration" class="anchor">declaration</a></h3><pre class="swift"><code><span class="comment">// declaring the function</span>
<span class="keyword">func</span> greet() {
<span class="call">print</span>(<span class="string">"Hello, World!"</span>)
}
<span class="comment">// calling the function</span>
<span class="call">greet</span>()
<span class="comment">// => Hello, World!</span></code></pre><div id="tooltip-5_1" class="tooltip"><textarea id="snippet-code-5_1" class="original-code">// declaring the function
func greet() {
print("Hello, World!")
}
// calling the function
greet()
// => Hello, World!</textarea><button id="copy-button-5_1" onclick="copySnippet('5_1')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID159" target="_blank">Defining and Calling Functions</a></p></div></div><div class="card"><div class="flex"><a id="functions/parameters"></a><h3><a href="#functions/parameters" class="anchor">parameters</a></h3><pre class="swift"><code><span class="comment">// explicit Void return type</span>
<span class="keyword">func</span> greet(name: <span class="type">String</span>) -> <span class="type">Void</span> {
<span class="call">print</span>(<span class="string">"Hello,</span> \(name)<span class="string">!"</span>)
}
<span class="call">greet</span>(name: <span class="string">"John"</span>)
<span class="comment">// => Hello, John!</span>
<span class="call">greet</span>(name: <span class="string">"Bob"</span>)
<span class="comment">// => Hello, Bob!</span></code></pre><div id="tooltip-5_2" class="tooltip"><textarea id="snippet-code-5_2" class="original-code">// explicit Void return type
func greet(name: String) -> Void {
print("Hello, \(name)!")
}
greet(name: "John")
// => Hello, John!
greet(name: "Bob")
// => Hello, Bob!</textarea><button id="copy-button-5_2" onclick="copySnippet('5_2')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID160" target="_blank">Function Parameters and Retrun Values</a></p></div></div><div class="card"><div class="flex"><a id="functions/return-values"></a><h3><a href="#functions/return-values" class="anchor">return values</a></h3><pre class="swift"><code><span class="comment">// returns with an Int type</span>
<span class="keyword">func</span> add(<span class="keyword">_</span> a: <span class="type">Int</span>, <span class="keyword">_</span> b: <span class="type">Int</span>) -> <span class="type">Int</span> {
<span class="keyword">return</span> a + b
}
<span class="comment">// implicit return</span>
<span class="keyword">func</span> greet(name: <span class="type">String</span>) -> <span class="type">String</span> {
<span class="string">"Hello,</span> \(name)<span class="string">!"</span>
}
<span class="call">print</span>(<span class="call">greet</span>(name: <span class="string">"John"</span>))
<span class="comment">// => Hello, John!</span>
<span class="call">print</span>(<span class="call">greet</span>(name: <span class="string">"Bob"</span>))
<span class="comment">// => Hello, Bob!</span></code></pre><div id="tooltip-5_3" class="tooltip"><textarea id="snippet-code-5_3" class="original-code">// returns with an Int type
func add(_ a: Int, _ b: Int) -> Int {
return a + b
}
// implicit return
func greet(name: String) -> String {
"Hello, \(name)!"
}
print(greet(name: "John"))
// => Hello, John!
print(greet(name: "Bob"))
// => Hello, Bob!</textarea><button id="copy-button-5_3" onclick="copySnippet('5_3')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID607" target="_blank">Functions With an Implicit Return</a></p></div></div><div class="card"><div class="flex"><a id="functions/argument-labels"></a><h3><a href="#functions/argument-labels" class="anchor">argument labels</a></h3><pre class="swift"><code><span class="comment">// omit argument labels</span>
<span class="keyword">func</span> add(<span class="keyword">_</span> a: <span class="type">Int</span>, <span class="keyword">_</span> b: <span class="type">Int</span>) -> <span class="type">Int</span> {
<span class="keyword">return</span> a + b
}
<span class="comment">// custom argument labels</span>
<span class="keyword">func</span> printSum(of a: <span class="type">Int</span>, and b: <span class="type">Int</span>) {
<span class="keyword">let</span> c = <span class="call">add</span>(a, b)
<span class="call">print</span>(<span class="string">"</span>\(a) <span class="string">+</span> \(b) <span class="string">=</span> \(c)<span class="string">"</span>)
}
<span class="call">printSum</span>(of: <span class="number">4</span>, and: <span class="number">2</span>)
<span class="comment">// => 4 + 2 = 6</span></code></pre><div id="tooltip-5_4" class="tooltip"><textarea id="snippet-code-5_4" class="original-code">// omit argument labels
func add(_ a: Int, _ b: Int) -> Int {
return a + b
}
// custom argument labels
func printSum(of a: Int, and b: Int) {
let c = add(a, b)
print("\(a) + \(b) = \(c)")
}
printSum(of: 4, and: 2)
// => 4 + 2 = 6</textarea><button id="copy-button-5_4" onclick="copySnippet('5_4')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID166" target="_blank">Argument Labels and Parameter Names</a></p></div></div><div class="card"><div class="flex"><a id="functions/variadic-functions"></a><h3><a href="#functions/variadic-functions" class="anchor">variadic functions</a></h3><pre class="swift"><code><span class="comment">// you can pass multiple Int values</span>
<span class="keyword">func</span> add(<span class="keyword">_</span> numbers: <span class="type">Int</span>...) -> <span class="type">Int</span> {
<span class="keyword">var</span> result = <span class="number">0</span>
<span class="comment">// numbers is an Array<Int> type</span>
<span class="keyword">for</span> number <span class="keyword">in</span> numbers {
result += number
}
<span class="keyword">return</span> result
}
<span class="call">print</span>(<span class="call">add</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>))
<span class="comment">// => 10</span></code></pre><div id="tooltip-5_5" class="tooltip"><textarea id="snippet-code-5_5" class="original-code">// you can pass multiple Int values
func add(_ numbers: Int...) -> Int {
var result = 0
// numbers is an Array<Int> type
for number in numbers {
result += number
}
return result
}
print(add(1,2,3,4))
// => 10</textarea><button id="copy-button-5_5" onclick="copySnippet('5_5')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID171" target="_blank">Variadic Parameters</a></p></div></div><div class="card"><div class="flex"><a id="functions/inout-parameters"></a><h3><a href="#functions/inout-parameters" class="anchor">inout parameters</a></h3><pre class="swift"><code><span class="keyword">func</span> increment(<span class="keyword">_</span> value: <span class="keyword">inout</span> <span class="type">Int</span>) {
value += <span class="number">1</span>
}
<span class="keyword">var</span> counter = <span class="number">0</span>
<span class="call">increment</span>(&counter)
<span class="call">print</span>(counter) <span class="comment">// => 1</span>
<span class="call">increment</span>(&counter)
<span class="call">print</span>(counter) <span class="comment">// => 2</span></code></pre><div id="tooltip-5_6" class="tooltip"><textarea id="snippet-code-5_6" class="original-code">func increment(_ value: inout Int) {
value += 1
}
var counter = 0
increment(&counter)
print(counter) // => 1
increment(&counter)
print(counter) // => 2</textarea><button id="copy-button-5_6" onclick="copySnippet('5_6')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID173" target="_blank">In-Out Parameters</a></p></div></div><div class="card"><div class="flex"><a id="functions/nested-functions"></a><h3><a href="#functions/nested-functions" class="anchor">nested functions</a></h3><pre class="swift"><code><span class="keyword">func</span> printSum(of a: <span class="type">Int</span>, and b: <span class="type">Int</span>) {
<span class="comment">// nested function</span>
<span class="keyword">func</span> add(<span class="keyword">_</span> a: <span class="type">Int</span>, <span class="keyword">_</span> b: <span class="type">Int</span>) -> <span class="type">Int</span> {
a + b
}
<span class="keyword">let</span> c = <span class="call">add</span>(a, b)
<span class="call">print</span>(<span class="string">"The sum is</span> \(c)<span class="string">"</span>)
}
<span class="call">printSum</span>(of: <span class="number">42</span>, and: <span class="number">6</span>) <span class="comment">// => 48</span></code></pre><div id="tooltip-5_7" class="tooltip"><textarea id="snippet-code-5_7" class="original-code">func printSum(of a: Int, and b: Int) {
// nested function
func add(_ a: Int, _ b: Int) -> Int {
a + b
}
let c = add(a, b)
print("The sum is \(c)")
}
printSum(of: 42, and: 6) // => 48</textarea><button id="copy-button-5_7" onclick="copySnippet('5_7')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html#ID178" target="_blank">Nested Functions</a></p></div></div><div class="card"><div class="flex"><a id="functions/overloading-parameters"></a><h3><a href="#functions/overloading-parameters" class="anchor">overloading parameters</a></h3><pre class="swift"><code><span class="keyword">func</span> add(<span class="keyword">_</span> a: <span class="type">Int</span>, <span class="keyword">_</span> b: <span class="type">Int</span>) -> <span class="type">Int</span> {
<span class="call">print</span>(<span class="string">"Add two Int values"</span>)
<span class="keyword">return</span> a + b
}
<span class="keyword">func</span> add(<span class="keyword">_</span> a: <span class="type">Float</span>, <span class="keyword">_</span> b: <span class="type">Float</span>) -> <span class="type">Float</span> {
<span class="call">print</span>(<span class="string">"Add two Float values"</span>)
<span class="keyword">return</span> a + b
}
<span class="call">print</span>(<span class="call">add</span>(<span class="number">42</span>, <span class="number">69</span>))
<span class="comment">// => Add two Int values
// => 111</span>
<span class="call">print</span>(<span class="call">add</span>(<span class="number">4.20</span>, <span class="number">6.9</span>))
<span class="comment">// => Add two Float values
// => 11.1</span></code></pre><div id="tooltip-5_8" class="tooltip"><textarea id="snippet-code-5_8" class="original-code">func add(_ a: Int, _ b: Int) -> Int {
print("Add two Int values")
return a + b
}
func add(_ a: Float, _ b: Float) -> Float {
print("Add two Float values")
return a + b
}
print(add(42, 69))
// => Add two Int values
// => 111
print(add(4.20, 6.9))
// => Add two Float values
// => 11.1</textarea><button id="copy-button-5_8" onclick="copySnippet('5_8')">Copy</button></div><p class="references">References: <a href="https://docs.swift.org/swift-book/LanguageGuide/Functions.html" target="_blank">Functions</a>, <a href="https://docs.swift.org/swift-book/LanguageGuide/Generics.html" target="_blank">Generics</a></p></div></div><div class="card"><div class="flex"><a id="functions/closures"></a><h3><a href="#functions/closures" class="anchor">closures</a></h3><pre class="swift"><code><span class="comment">// closure block</span>
<span class="keyword">let</span> log: (<span class="type">Int</span>) -> <span class="type">Void</span> = { value <span class="keyword">in</span>
<span class="call">print</span>(<span class="string">"Value:</span> \(value)<span class="string">"</span>)