-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClosures.html
More file actions
757 lines (629 loc) · 52.4 KB
/
Copy pathClosures.html
File metadata and controls
757 lines (629 loc) · 52.4 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
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Swift Programming Language: Closures</title>
<link rel="stylesheet" type="text/css" href="resource/style-1.1.15.css">
<meta charset="utf-8"> <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
<body id="conceptual_flow_with_tasks" class="jazz">
<div id="_omniture_top">
</div>
<a name="TP40014097" title="The Swift Programming Language"></a>
<section id="valence">
<div class="content-wrapper">
<p id="hierarchial_navigation">
<span id="book_title">The Swift Programming Language</span>
</p>
<img id="shortstack" src="./resource/shortstack_2x.png">
</div>
</section>
<div class="content-wrapper">
<nav class="book-parts hideInXcode" role="navigation">
<ul class="nav-parts">
<li data-id="TP40014097-CH1-XID_27" class="part-name">Welcome to Swift
<ul class="nav-chapters">
<li class="nav-chapter nav-visited-chapter">
<a href="About Swift.html#TP40014097-CH3-XID_0" data-id="TP40014097-CH3-XID_0" class="">About Swift</a>
</li>
<li class="nav-chapter">
<a href="A Swift Tour.html#TP40014097-CH2-XID_1" data-id="TP40014097-CH2-XID_1" class="">A Swift Tour</a>
</li>
</ul>
</li><li data-id="TP40014097-CH4-XID_299" class="part-name nav-part-active open-part">Language Guide
<ul class="nav-chapters" style="height: 607px;">
<li class="nav-chapter">
<a href="The Basics.html" data-id="TP40014097-CH5-XID_399" >The Basics</a>
</li>
<li class="nav-chapter">
<a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70" class="">Basic Operators</a>
</li>
<li class="nav-chapter">
<a href="Strings and Characters.html#TP40014097-CH7-XID_368" data-id="TP40014097-CH7-XID_368">Strings and Characters</a>
</li>
<li class="nav-chapter ">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133">Collection Types</a>
</li>
<li class="nav-chapter ">
<a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153" >Control Flow</a>
</li>
<li class="nav-chapter">
<a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204">Functions</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="Closures.html#TP40014097-CH11-XID_117" data-id="TP40014097-CH11-XID_117" class="nav-chapter-active">Closures</a>
</li>
<li class="nav-chapter">
<a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185">Enumerations</a>
</li>
<li class="nav-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94">Classes and Structures</a>
</li>
<li class="nav-chapter">
<a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323">Properties</a>
</li>
<li class="nav-chapter">
<a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300">Methods</a>
</li>
<li class="nav-chapter">
<a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393">Subscripts</a>
</li>
<li class="nav-chapter">
<a href="Inheritance.html#TP40014097-CH17-XID_251" data-id="TP40014097-CH17-XID_251">Inheritance</a>
</li>
<li class="nav-chapter">
<a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a>
</li>
<li class="nav-chapter">
<a href="Deinitialization.html#TP40014097-CH19-XID_182" data-id="TP40014097-CH19-XID_182">Deinitialization</a>
</li>
<li class="nav-chapter">
<a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50">Automatic Reference Counting</a>
</li>
<li class="nav-chapter">
<a href="Optional Chaining.html#TP40014097-CH21-XID_312" data-id="TP40014097-CH21-XID_312">Optional Chaining</a>
</li>
<li class="nav-chapter">
<a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443">Type Casting</a>
</li>
<li class="nav-chapter">
<a href="Nested Types.html#TP40014097-CH23-XID_309" data-id="TP40014097-CH23-XID_309">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name">Language Reference
<ul class="nav-chapters">
<li class="nav-chapter">
<a href="About the Language Reference.html#TP40014097-CH29-XID_453" data-id="TP40014097-CH29-XID_453" class="">About the Language Reference</a>
</li>
<li class="nav-chapter">
<a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794">Lexical Structure</a>
</li>
<li class="nav-chapter">
<a href="Types.html#TP40014097-CH31-XID_988" data-id="TP40014097-CH31-XID_988">Types</a>
</li>
<li class="nav-chapter">
<a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655">Expressions</a>
</li>
<li class="nav-chapter">
<a href="Statements.html#TP40014097-CH33-XID_913" data-id="TP40014097-CH33-XID_913">Statements</a>
</li>
<li class="nav-chapter">
<a href="Declarations.html#TP40014097-CH34-XID_475" data-id="TP40014097-CH34-XID_475">Declarations</a>
</li>
<li class="nav-chapter">
<a href="Attributes.html#TP40014097-CH35-XID_460" data-id="TP40014097-CH35-XID_460">Attributes</a>
</li>
<li class="nav-chapter">
<a href="Patterns.html#TP40014097-CH36-XID_878" data-id="TP40014097-CH36-XID_878">Patterns</a>
</li>
<li class="nav-chapter">
<a href="Generic Parameters and Arguments.html#TP40014097-CH37-XID_774" data-id="TP40014097-CH37-XID_774">Generic Parameters and Arguments</a>
</li>
<li class="nav-chapter">
<a href="Summary of the Grammar.html#TP40014097-CH38-XID_1030" data-id="TP40014097-CH38-XID_1030">Summary of the Grammar</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="chapter">
<a name="TP40014097-CH11"></a><a name="TP40014097-CH11-XID_117"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Closures</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-CH11-XID_119">
Closure Expressions
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH11-XID_126">
Trailing Closures
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH11-XID_129">
Capturing Values
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH11-XID_132">
Closures Are Reference Types
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
<em>Closures</em> are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.
</p>
<p class="para">
Closures can capture and store references to any constants and variables from the context in which they are defined. This is known as <em>closing</em> over those constants and variables, hence the name “closures”. Swift handles all of the memory management of capturing for you.
</p>
<div class="note">
<a name="TP40014097-CH11-XID_118"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Don’t worry if you are not familiar with the concept of “capturing”. It is explained in detail below in <span class="x-name"><a href="#TP40014097-CH11-XID_129" data-id="TP40014097-CH11-XID_129">Capturing Values</a></span>.
</p>
</aside>
</div>
<p class="para">
Global and nested functions, as introduced in <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204">Functions</a></span>, are actually special cases of closures. Closures take one of three forms:
</p>
<ul class="list-bullet">
<li class="item"><p class="para">
Global functions are closures that have a name and do not capture any values.
</p>
</li><li class="item"><p class="para">
Nested functions are closures that have a name and can capture values from their enclosing function.
</p>
</li><li class="item"><p class="para">
Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context.
</p>
</li>
</ul>
<p class="para">
Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios. These optimizations include:
</p>
<ul class="list-bullet">
<li class="item"><p class="para">
Inferring parameter and return value types from context
</p>
</li><li class="item"><p class="para">
Implicit returns from single-expression closures
</p>
</li><li class="item"><p class="para">
Shorthand argument names
</p>
</li><li class="item"><p class="para">
Trailing closure syntax
</p>
</li>
</ul>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_119"></a>
<h3 class="section-name" tabindex="0">Closure Expressions</h3>
<p class="para">
Nested functions, as introduced in <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_233" data-id="TP40014097-CH10-XID_233">Nested Functions</a></span>, are a convenient means of naming and defining self-contained blocks of code as part of a larger function. However, it is sometimes useful to write shorter versions of function-like constructs without a full declaration and name. This is particularly true when you work with functions that take other functions as one or more of their arguments.
</p><p class="para">
<em>Closure expressions</em> are a way to write inline closures in a brief, focused syntax. Closure expressions provide several syntax optimizations for writing closures in their simplest form without loss of clarity or intent. The closure expression examples below illustrate these optimizations by refining a single example of the <code class="code-voice">sort</code> function over several iterations, each of which expresses the same functionality in a more succinct way.
</p>
<section class="section">
<a name="TP40014097-CH11-XID_120"></a>
<h3 class="section-name" tabindex="0">The Sort Function</h3>
<p class="para">
Swift’s standard library provides a function called <code class="code-voice">sort</code>, which sorts an array of values of a known type, based on the output of a sorting closure that you provide. Once it completes the sorting process, the <code class="code-voice">sort</code> function returns a new array of the same type and size as the old one, with its elements in the correct sorted order.
</p><p class="para">
The closure expression examples below use the <code class="code-voice">sort</code> function to sort an array of <code class="code-voice">String</code> values in reverse alphabetical order. Here’s the initial array to be sorted:
</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">names</span> = [<span class="s">"Chris"</span>, <span class="s">"Alex"</span>, <span class="s">"Ewa"</span>, <span class="s">"Barry"</span>, <span class="s">"Daniella"</span>]</code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">sort</code> function takes two arguments:
</p><ul class="list-bullet">
<li class="item"><p class="para">
An array of values of a known type.
</p>
</li><li class="item"><p class="para">
A closure that takes two arguments of the same type as the array’s contents, and returns a <code class="code-voice">Bool</code> value to say whether the first value should appear before or after the second value once the values are sorted. The sorting closure needs to return <code class="code-voice">true</code> if the first value should appear <em>before</em> the second value, and <code class="code-voice">false</code> otherwise.
</p>
</li>
</ul><p class="para">
This example is sorting an array of <code class="code-voice">String</code> values, and so the sorting closure needs to be a function of type <code class="code-voice">(String, String) -> Bool</code>.
</p><p class="para">
One way to provide the sorting closure is to write a normal function of the correct type, and to pass it in as the <code class="code-voice">sort</code> function’s second parameter:
</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">func</span> <span class="vc">backwards</span>(<span class="vc">s1</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">s1</span> > <span class="vc">s2</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="kt">var</span> <span class="vc">reversed</span> = <span class="vc">sort</span>(<span class="vc">names</span>, <span class="vc">backwards</span>)</code></li>
<li><code class="code-voice"><span class="c">// reversed is equal to ["Ewa", "Daniella", "Chris", "Barry", "Alex"]</span></code></li>
</ul>
</div>
</section><p class="para">
If the first string (<code class="code-voice">s1</code>) is greater than the second string (<code class="code-voice">s2</code>), the <code class="code-voice">backwards</code> function will return <code class="code-voice">true</code>, indicating that <code class="code-voice">s1</code> should appear before <code class="code-voice">s2</code> in the sorted array. For characters in strings, “greater than” means “appears later in the alphabet than”. This means that the letter <code class="code-voice">"B"</code> is “greater than” the letter <code class="code-voice">"A"</code>, and the string <code class="code-voice">"Tom"</code> is greater than the string <code class="code-voice">"Tim"</code>. This gives a reverse alphabetical sort, with <code class="code-voice">"Barry"</code> being placed before <code class="code-voice">"Alex"</code>, and so on.
</p><p class="para">
However, this is a rather long-winded way to write what is essentially a single-expression function (<code class="code-voice">a > b</code>). In this example, it would be preferable to write the sorting closure inline, using closure expression syntax.
</p>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_121"></a>
<h3 class="section-name" tabindex="0">Closure Expression Syntax</h3>
<p class="para">
Closure expression syntax has the following general form:
</p><span class="caption"></span>
<div class="code-outline">
<ul class="code-outline-lines">
<li><pre class="code-voice">{ (<em class="variable-text">parameters</em>) -> <em class="variable-text">return type</em> <span class="kt">in</span></pre></li><li><pre class="code-voice"> <em class="variable-text">statements</em></pre></li><li><pre class="code-voice">}</pre></li>
</ul>
</div><p class="para">
Closure expression syntax can use constant parameters, variable parameters, and <code class="code-voice">inout</code> parameters. Default values cannot be provided. Variadic parameters can be used if you name the variadic parameter and place it last in the parameter list. Tuples can also be used as parameter types and return types.
</p><p class="para">
The example below shows a closure expression version of the <code class="code-voice">backwards</code> function from earlier:
</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">reversed</span> = <span class="vc">sort</span>(<span class="vc">names</span>, { (<span class="vc">s1</span>: <span class="n"></span>) -> <span class="vc">Bool</span> <span class="kt">in</span></code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">s1</span> > <span class="vc">s2</span></code></li>
<li><code class="code-voice"> })</code></li>
</ul>
</div>
</section><p class="para">
Note that the declaration of parameters and return type for this inline closure is identical to the declaration from the <code class="code-voice">backwards</code> function. In both cases, it is written as <code class="code-voice">(s1: String, s2: String) -> Bool</code>. However, for the inline closure expression, the parameters and return type are written <em>inside</em> the curly braces, not outside of them.
</p><p class="para">
The start of the closure’s body is introduced by the <code class="code-voice">in</code> keyword. This keyword indicates that the definition of the closure’s parameters and return type has finished, and the body of the closure is about to begin.
</p><p class="para">
Because the body of the closure is so short, it can even be written on a single line:
</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">reversed</span> = <span class="vc">sort</span>(<span class="vc">names</span>, { (<span class="vc">s1</span>: <span class="n"></span>) -> <span class="vc">Bool</span> <span class="kt">in</span> <span class="kt">return</span> <span class="vc">s1</span> > <span class="vc">s2</span> } )</code></li>
</ul>
</div>
</section><p class="para">
This illustrates that the overall call to the <code class="code-voice">sort</code> function has remained the same. A pair of parentheses still wrap the entire set of arguments for the function. However, one of those arguments is now an inline closure.
</p>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_122"></a>
<h3 class="section-name" tabindex="0">Inferring Type From Context</h3>
<p class="para">
Because the sorting closure is passed as an argument to a function, Swift can infer the types of its parameters and the type of the value it returns from the type of the <code class="code-voice">sort</code> function’s second parameter. This parameter is expecting a function of type <code class="code-voice">(String, String) -> Bool</code>. This means that the <code class="code-voice">String</code>, <code class="code-voice">String</code>, and <code class="code-voice">Bool</code> types do not need to be written as part of the closure expression’s definition. Because all of the types can be inferred, the return arrow (<code class="code-voice">-></code>) and the parentheses around the names of the parameters can also be omitted:
</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">reversed</span> = <span class="vc">sort</span>(<span class="vc">names</span>, { <span class="vc">s1</span>, <span class="vc">s2</span> <span class="kt">in</span> <span class="kt">return</span> <span class="vc">s1</span> > <span class="vc">s2</span> } )</code></li>
</ul>
</div>
</section><p class="para">
It is always possible to infer parameter types and return type when passing a closure to a function as an inline closure expression. As a result, you rarely need to write an inline closure in its fullest form.
</p><p class="para">
Nonetheless, you can make the types explicit if you wish, and doing so is encouraged if it avoids ambiguity for readers of your code. In the case of the <code class="code-voice">sort</code> function, the purpose of the closure is clear from the fact that sorting is taking place, and it is safe for a reader to assume that the closure is likely to be working with <code class="code-voice">String</code> values, because it is assisting with the sorting of an array of strings.
</p>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_123"></a>
<h3 class="section-name" tabindex="0">Implicit Returns from Single-Expression Closures</h3>
<p class="para">
Single-expression closures can implicitly return the result of their single expression by omitting the <code class="code-voice">return</code> keyword from their declaration, as in this version of the previous example:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="vc">reversed</span> = <span class="vc">sort</span>(<span class="vc">names</span>, { <span class="vc">s1</span>, <span class="vc">s2</span> <span class="kt">in</span> <span class="vc">s1</span> > <span class="vc">s2</span> } )</code></li>
</ul>
</div>
</section><p class="para">
Here, the function type of the <code class="code-voice">sort</code> function’s second argument makes it clear that a <code class="code-voice">Bool</code> value must be returned by the closure. Because the closure’s body contains a single expression (<code class="code-voice">s1 > s2</code>) that returns a <code class="code-voice">Bool</code> value, there is no ambiguity, and the <code class="code-voice">return</code> keyword can be omitted.
</p>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_124"></a>
<h3 class="section-name" tabindex="0">Shorthand Argument Names</h3>
<p class="para">
Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names <code class="code-voice">$0</code>, <code class="code-voice">$1</code>, <code class="code-voice">$2</code>, and so on.
</p><p class="para">
If you use these shorthand argument names within your closure expression, you can omit the closure’s argument list from its definition, and the number and type of the shorthand argument names will be inferred from the expected function type. The <code class="code-voice">in</code> keyword can also be omitted, because the closure expression is made up entirely of its body:
</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">reversed</span> = <span class="vc">sort</span>(<span class="vc">names</span>, { <span class="vc">$0</span> > <span class="vc">$1</span> } )</code></li>
</ul>
</div>
</section><p class="para">
Here, <code class="code-voice">$0</code> and <code class="code-voice">$1</code> refer to the closure’s first and second <code class="code-voice">String</code> arguments.
</p>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_125"></a>
<h3 class="section-name" tabindex="0">Operator Functions</h3>
<p class="para">
There’s actually an even <em>shorter</em> way to write the closure expression above. Swift’s <code class="code-voice">String</code> type defines its string-specific implementation of the greater-than operator (<code class="code-voice">></code>) as a function that has two parameters of type <code class="code-voice">String</code>, and returns a value of type <code class="code-voice">Bool</code>. This exactly matches the function type needed for the <code class="code-voice">sort</code> function’s second parameter. Therefore, you can simply pass in the greater-than operator, and Swift will infer that you want to use its string-specific implementation:
</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">reversed</span> = <span class="vc">sort</span>(<span class="vc">names</span>, >)</code></li>
</ul>
</div>
</section><p class="para">
For more about operator functions, see <span class="x-name"><a href="Advanced Operators.html#TP40014097-CH27-XID_43" data-id="TP40014097-CH27-XID_43">Operator Functions</a></span>.
</p>
</section>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_126"></a>
<h3 class="section-name" tabindex="0">Trailing Closures</h3>
<p class="para">
If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a <em>trailing closure</em> instead. A trailing closure is a closure expression that is written outside of (and <em>after</em>) the parentheses of the function call it supports:
</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">func</span> <span class="vc">someFunctionThatTakesAClosure</span>(<span class="vc">closure</span>: () -> ()) {</code></li>
<li><code class="code-voice"> <span class="c">// function body goes here</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="c">// here's how you call this function without using a trailing closure:</span></code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="vc">someFunctionThatTakesAClosure</span>({</code></li>
<li><code class="code-voice"> <span class="c">// closure's body goes here</span></code></li>
<li><code class="code-voice"> })</code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="c">// here's how you call this function with a trailing closure instead:</span></code></li>
<li><code class="code-voice"> </code></li>
<li><code class="code-voice"><span class="vc">someFunctionThatTakesAClosure</span>() {</code></li>
<li><code class="code-voice"> <span class="c">// trailing closure's body goes here</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH11-XID_127"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">If a closure expression is provided as the function’s only argument and you provide that expression as a trailing closure, you do not need to write a pair of parentheses <code class="code-voice">()</code> after the function’s name when you call the function.
</p>
</aside>
</div><p class="para">
The string-sorting closure from the <span class="x-name"><a href="#TP40014097-CH11-XID_121" data-id="TP40014097-CH11-XID_121">Closure Expression Syntax</a></span> section above can be written outside of the <code class="code-voice">sort</code> function’s parentheses as a trailing closure:
</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">reversed</span> = <span class="vc">sort</span>(<span class="vc">names</span>) { <span class="vc">$0</span> > <span class="vc">$1</span> }</code></li>
</ul>
</div>
</section><p class="para">
Trailing closures are most useful when the closure is sufficiently long that it is not possible to write it inline on a single line. As an example, Swift’s <code class="code-voice">Array</code> type has a <code class="code-voice">map</code> method which takes a closure expression as its single argument. The closure is called once for each item in the array, and returns an alternative mapped value (possibly of some other type) for that item. The nature of the mapping and the type of the returned value is left up to the closure to specify.
</p><p class="para">
After applying the provided closure to each array element, the <code class="code-voice">map</code> method returns a new array containing all of the new mapped values, in the same order as their corresponding values in the original array.
</p><p class="para">
Here’s how you can use the <code class="code-voice">map</code> method with a trailing closure to convert an array of <code class="code-voice">Int</code> values into an array of <code class="code-voice">String</code> values. The array <code class="code-voice">[16, 58, 510]</code> is used to create the new array <code class="code-voice">["OneSix", "FiveEight", "FiveOneZero"]</code>:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">digitNames</span> = [</code></li>
<li><code class="code-voice"> <span class="m">0</span>: <span class="s">"Zero"</span>, <span class="m">1</span>: <span class="s">"One"</span>, <span class="m">2</span>: <span class="s">"Two"</span>, <span class="m">3</span>: <span class="s">"Three"</span>, <span class="m">4</span>: <span class="s">"Four"</span>,</code></li>
<li><code class="code-voice"> <span class="m">5</span>: <span class="s">"Five"</span>, <span class="m">6</span>: <span class="s">"Six"</span>, <span class="m">7</span>: <span class="s">"Seven"</span>, <span class="m">8</span>: <span class="s">"Eight"</span>, <span class="m">9</span>: <span class="s">"Nine"</span></code></li>
<li><code class="code-voice">]</code></li>
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">numbers</span> = [<span class="m">16</span>, <span class="m">58</span>, <span class="m">510</span>]</code></li>
</ul>
</div>
</section><p class="para">
The code above creates a dictionary of mappings between the integer digits and English-language versions of their names. It also defines an array of integers, ready to be converted into strings.
</p><p class="para">
You can now use the <code class="code-voice">numbers</code> array to create an array of <code class="code-voice">String</code> values, by passing a closure expression to the array’s <code class="code-voice">map</code> method as a trailing closure. Note that the call to <code class="code-voice">numbers.map</code> does not need to include any parentheses after <code class="code-voice">map</code>, because the <code class="code-voice">map</code> method has only one parameter, and that parameter is provided as a trailing closure:
</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">strings</span> = <span class="vc">numbers</span>.<span class="vc">map</span> {</code></li>
<li><code class="code-voice"> (<span class="kt">var</span> <span class="vc">number</span>) -> <span class="vc">String</span> <span class="kt">in</span></code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">output</span> = <span class="s">""</span></code></li>
<li><code class="code-voice"> <span class="kt">while</span> <span class="vc">number</span> > <span class="m">0</span> {</code></li>
<li><code class="code-voice"> <span class="vc">output</span> = <span class="vc">digitNames</span>[<span class="vc">number</span> % <span class="m">10</span>]! + <span class="vc">output</span></code></li>
<li><code class="code-voice"> <span class="vc">number</span> /= <span class="m">10</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">output</span></code></li>
<li><code class="code-voice">}</code></li>
<li><code class="code-voice"><span class="c">// strings is inferred to be of type String[]</span></code></li>
<li><code class="code-voice"><span class="c">// its value is ["OneSix", "FiveEight", "FiveOneZero"]</span></code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">map</code> function calls the closure expression once for each item in the array. You do not need to specify the type of the closure’s input parameter, <code class="code-voice">number</code>, because the type can be inferred from the values in the array to be mapped.
</p><p class="para">
In this example, the closure’s <code class="code-voice">number</code> parameter is defined as a <em>variable parameter</em>, as described in <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_224" data-id="TP40014097-CH10-XID_224">Constant and Variable Parameters</a></span>, so that the parameter’s value can be modified within the closure body, rather than declaring a new local variable and assigning the passed <code class="code-voice">number</code> value to it. The closure expression also specifies a return type of <code class="code-voice">String</code>, to indicate the type that will be stored in the mapped output array.
</p><p class="para">
The closure expression builds a string called <code class="code-voice">output</code> each time it is called. It calculates the last digit of <code class="code-voice">number</code> by using the remainder operator (<code class="code-voice">number % 10</code>), and uses this digit to look up an appropriate string in the <code class="code-voice">digitNames</code> dictionary.
</p><div class="note">
<a name="TP40014097-CH11-XID_128"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">The call to the <code class="code-voice">digitNames</code> dictionary’s subscript is followed by an exclamation mark (<code class="code-voice">!</code>), because dictionary subscripts return an optional value to indicate that the dictionary lookup can fail if the key does not exist. In the example above, it is guaranteed that <code class="code-voice">number % 10</code> will always be a valid subscript key for the <code class="code-voice">digitNames</code> dictionary, and so an exclamation mark is used to force-unwrap the <code class="code-voice">String</code> value stored in the subscript’s optional return value.
</p>
</aside>
</div><p class="para">
The string retrieved from the <code class="code-voice">digitNames</code> dictionary is added to the <em>front</em> of <code class="code-voice">output</code>, effectively building a string version of the number in reverse. (The expression <code class="code-voice">number % 10</code> gives a value of <code class="code-voice">6</code> for <code class="code-voice">16</code>, <code class="code-voice">8</code> for <code class="code-voice">58</code>, and <code class="code-voice">0</code> for <code class="code-voice">510</code>.)
</p><p class="para">
The <code class="code-voice">number</code> variable is then divided by <code class="code-voice">10</code>. Because it is an integer, it is rounded down during the division, so <code class="code-voice">16</code> becomes <code class="code-voice">1</code>, <code class="code-voice">58</code> becomes <code class="code-voice">5</code>, and <code class="code-voice">510</code> becomes <code class="code-voice">51</code>.
</p><p class="para">
The process is repeated until <code class="code-voice">number /= 10</code> is equal to <code class="code-voice">0</code>, at which point the <code class="code-voice">output</code> string is returned by the closure, and is added to the output array by the <code class="code-voice">map</code> function.
</p><p class="para">
The use of trailing closure syntax in the example above neatly encapsulates the closure’s functionality immediately after the function that closure supports, without needing to wrap the entire closure within the <code class="code-voice">map</code> function’s outer parentheses.
</p>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_129"></a>
<h3 class="section-name" tabindex="0">Capturing Values</h3>
<p class="para">
A closure can <em>capture</em> constants and variables from the surrounding context in which it is defined. The closure can then refer to and modify the values of those constants and variables from within its body, even if the original scope that defined the constants and variables no longer exists.
</p><p class="para">
The simplest form of a closure in Swift is a nested function, written within the body of another function. A nested function can capture any of its outer function’s arguments and can also capture any constants and variables defined within the outer function.
</p><p class="para">
Here’s an example of a function called <code class="code-voice">makeIncrementor</code>, which contains a nested function called <code class="code-voice">incrementor</code>. The nested <code class="code-voice">incrementor</code> function captures two values, <code class="code-voice">runningTotal</code> and <code class="code-voice">amount</code>, from its surrounding context. After capturing these values, <code class="code-voice">incrementor</code> is returned by <code class="code-voice">makeIncrementor</code> as a closure that increments <code class="code-voice">runningTotal</code> by <code class="code-voice">amount</code> each time it is called.
</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">func</span> <span class="vc">makeIncrementor</span>(<span class="vc">forIncrement</span> <span class="vc">amount</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">runningTotal</span> = <span class="m">0</span></code></li>
<li><code class="code-voice"> <span class="kt">func</span> <span class="vc">incrementor</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="vc">runningTotal</span> += <span class="vc">amount</span></code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">runningTotal</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">incrementor</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The return type of <code class="code-voice">makeIncrementor</code> is <code class="code-voice">() -> Int</code>. This means that it returns a <em>function</em>, rather than a simple value. The function it returns has no parameters, and returns an <code class="code-voice">Int</code> value each time it is called. To learn how functions can return other functions, see <span class="x-name"><a href="Functions.html#TP40014097-CH10-XID_232" data-id="TP40014097-CH10-XID_232">Function Types as Return Types</a></span>.
</p><p class="para">
The <code class="code-voice">makeIncrementor</code> function defines an integer variable called <code class="code-voice">runningTotal</code>, to store the current running total of the incrementor that will be returned. This variable is initialized with a value of <code class="code-voice">0</code>.
</p><p class="para">
The <code class="code-voice">makeIncrementor</code> function has a single <code class="code-voice">Int</code> parameter with an external name of <code class="code-voice">forIncrement</code>, and a local name of <code class="code-voice">amount</code>. The argument value passed to this parameter specifies how much <code class="code-voice">runningTotal</code> should be incremented by each time the returned incrementor function is called.
</p><p class="para">
<code class="code-voice">makeIncrementor</code> defines a nested function called <code class="code-voice">incrementor</code>, which performs the actual incrementing. This function simply adds <code class="code-voice">amount</code> to <code class="code-voice">runningTotal</code>, and returns the result.
</p><p class="para">
When considered in isolation, the nested <code class="code-voice">incrementor</code> function might seem unusual:
</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">func</span> <span class="vc">incrementor</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="vc">runningTotal</span> += <span class="vc">amount</span></code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="vc">runningTotal</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">incrementor</code> function doesn’t have any parameters, and yet it refers to <code class="code-voice">runningTotal</code> and <code class="code-voice">amount</code> from within its function body. It does this by capturing the <em>existing</em> values of <code class="code-voice">runningTotal</code> and <code class="code-voice">amount</code> from its surrounding function and using them within its own function body.
</p><p class="para">
Because it does not modify <code class="code-voice">amount</code>, <code class="code-voice">incrementor</code> actually captures and stores a <em>copy</em> of the value stored in <code class="code-voice">amount</code>. This value is stored along with the new <code class="code-voice">incrementor</code> function.
</p><p class="para">
However, because it modifies the <code class="code-voice">runningTotal</code> variable each time it is called, <code class="code-voice">incrementor</code> captures a <em>reference</em> to the current <code class="code-voice">runningTotal</code> variable, and not just a copy of its initial value. Capturing a reference ensures sure that <code class="code-voice">runningTotal</code> does not disappear when the call to <code class="code-voice">makeIncrementor</code> ends, and ensures that <code class="code-voice">runningTotal</code> will continue to be available the next time that the incrementor function is called.
</p><div class="note">
<a name="TP40014097-CH11-XID_130"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Swift determines what should be captured by reference and what should be copied by value. You don’t need to annotate <code class="code-voice">amount</code> or <code class="code-voice">runningTotal</code> to say that they can be used within the nested <code class="code-voice">incrementor</code> function. Swift also handles all memory management involved in disposing of <code class="code-voice">runningTotal</code> when it is no longer needed by the incrementor function.
</p>
</aside>
</div><p class="para">
Here’s an example of <code class="code-voice">makeIncrementor</code> in action:
</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">incrementByTen</span> = <span class="vc">makeIncrementor</span>(<span class="vc">forIncrement</span>: <span class="m">10</span>)</code></li>
</ul>
</div>
</section><p class="para">
This example sets a constant called <code class="code-voice">incrementByTen</code> to refer to an incrementor function that adds <code class="code-voice">10</code> to its <code class="code-voice">runningTotal</code> variable each time it is called. Calling the function multiple times shows this behavior in action:
</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">incrementByTen</span>()</code></li>
<li><code class="code-voice"><span class="c">// returns a value of 10</span></code></li>
<li><code class="code-voice"><span class="vc">incrementByTen</span>()</code></li>
<li><code class="code-voice"><span class="c">// returns a value of 20</span></code></li>
<li><code class="code-voice"><span class="vc">incrementByTen</span>()</code></li>
<li><code class="code-voice"><span class="c">// returns a value of 30</span></code></li>
</ul>
</div>
</section><p class="para">
If you create another incrementor, it will have its own stored reference to a new, separate <code class="code-voice">runningTotal</code> variable. In the example below, <code class="code-voice">incrementBySeven</code> captures a reference to a new <code class="code-voice">runningTotal</code> variable, and this variable is unconnected to the one captured by <code class="code-voice">incrementByTen</code>:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">incrementBySeven</span> = <span class="vc">makeIncrementor</span>(<span class="vc">forIncrement</span>: <span class="m">7</span>)</code></li>
<li><code class="code-voice"><span class="vc">incrementBySeven</span>()</code></li>
<li><code class="code-voice"><span class="c">// returns a value of 7</span></code></li>
<li><code class="code-voice"><span class="vc">incrementByTen</span>()</code></li>
<li><code class="code-voice"><span class="c">// returns a value of 40</span></code></li>
</ul>
</div>
</section><div class="note">
<a name="TP40014097-CH11-XID_131"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">If you assign a closure to a property of a class instance, and the closure captures that instance by referring to the instance or its members, you will create a strong reference cycle between the closure and the instance. Swift uses <em>capture lists</em> to break these strong reference cycles. For more information, see <span class="x-name"><a href="Automatic Reference Counting.html#TP40014097-CH20-XID_61" data-id="TP40014097-CH20-XID_61">Strong Reference Cycles for Closures</a></span>.
</p>
</aside>
</div>
</section>
<section class="section">
<a name="TP40014097-CH11-XID_132"></a>
<h3 class="section-name" tabindex="0">Closures Are Reference Types</h3>
<p class="para">
In the example above, <code class="code-voice">incrementBySeven</code> and <code class="code-voice">incrementByTen</code> are constants, but the closures these constants refer to are still able to increment the <code class="code-voice">runningTotal</code> variables that they have captured. This is because functions and closures are <em>reference types</em>.
</p><p class="para">
Whenever you assign a function or a closure to a constant or a variable, you are actually setting that constant or variable to be a <em>reference</em> to the function or closure. In the example above, it is the choice of closure that <code class="code-voice">incrementByTen</code> <em>refers to</em> that is constant, and not the contents of the closure itself.
</p><p class="para">
This also means that if you assign a closure to two different constants or variables, both of those constants or variables will refer to the same closure:
</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">alsoIncrementByTen</span> = <span class="vc">incrementByTen</span></code></li>
<li><code class="code-voice"><span class="vc">alsoIncrementByTen</span>()</code></li>
<li><code class="code-voice"><span class="c">// returns a value of 50</span></code></li>
</ul>
</div>
</section>
</section>
<section id="next_previous" class="">
<p class="previous-link"><a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204">Functions</a></p>
<p class="next-link"><a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185">Enumerations</a></p>
<p class="copyright">Copyright © 2014 Apple Inc. All rights reserved. <a class="link" href="http://www.apple.com/legal/terms/site.html" target="_blank" rel="external">Terms of Use</a> | <a class="link" href="http://www.apple.com/privacy/" target="_blank" rel="external">Privacy Policy</a> | Updated: 2014-06-02
</p>
</section>
</article>
</div>
<div id="big_button"></div>
<footer id="footer">
<div class="content-wrapper">
</div>
<script src="./resource/devpubs-1.1.15.js" type="text/javascript" charset="utf-8"></script> </footer> <div id="blackout"> <div id="preload"></div> </div> </body></html>