Swift Capture Lists

Introduction

Welcome to Lesson 4 of Intro to Functional Swift. Today, you will discover a unique behavior of closures, and how to make it right.

Discovery Stage

First, let us create two variables.

  1. var a = 0
  2. var b = 0

Design Closure

Create a closure block which prints a and b.

  1. let newClosure = { print(a, b) }
  2. newClosure() // (0, 0)

Design Closure Array

Create an array whose type is [() -> ()]. newClosure will be added to closureArray.

  1. var closureArray: [() -> ()] = []
  2. var i = 0

Append Closure

Let us add multiple closure blocks to the array using a for-in loop.

  1. for _ in 1...5 {
  2. closureArray.append {
  3. print(i)
  4. }
  5. i += 1
  6. }
  7. // i is 5 by now

Execute Closure

Let us call the function.

  1. closureArray[0]() // 5 ?
  2. closureArray[1]() // 5 ?
  3. closureArray[2]() // 5 ?
  4. closureArray[3]() // 5 ?
  5. closureArray[4]() // 5 ?

You might have expected each element to print, 0, 1, 2, 3, 4. However, each closure uses the final value of i which is 5 at the end of the loop.

Important: A closure block is a reference type. You will learn more in Chapter 5.

Characteristic of Closure

Let us dig deeper.

Design Closure

  1. var c = 0
  2. var d = 0
  3. let smartClosure: () -> () = { _ in
  4. print(c, d)
  5. }

First, modify the value of c and d. Second execute the closure.

  1. c = 9
  2. d = 9
  3. smartClosure() // (9, 9)

smartClosure() prints (9, 9). When the value of c and d mutate, the change is reflected on the closure by default.

Introducing Capture List

Let us destroy the default behavior.

Strategy: Do not reference. Copy

Add an array [a, b] before in. The array is referred to as a capture list.

  1. let smarterClosure: () -> () = { [c, d] in
  2. print(c, d)
  3. }
  4. smarterClosure() // (9, 9)

Now , attempt to modify the value of c and d

  1. c = 10
  2. d = 10

Execute smarterClosure. You will discover that the change is not reflected on the closure anymore.

  1. smarterClosure() // (9, 9)

The capture list “copies” the value of c and d at the particular time the closure was created. It become invincible.

Destroy Unique Behavior

Let us go back to the early example with the for-in loop to append the closure block to the array.

Design Closure

  1. var smartClosureArray: [() -> ()] = []
  2. var j = 0

Append Closure

Capture j

  1. for _ in 1...5 {
  2. smartClosureArray.append { [j] in
  3. print(j)
  4. }
  5. j += 1
  6. }

You may rebrand j. In this case, I will refer to j as num within the closure block.

  1. for _ in 1...5 {
  2. smartClosureArray.append { [num = j] in
  3. print(num)
  4. }
  5. j += 1
  6. }

Execute

Let us find out whether you’ve killed the default behavior.

  1. smartClosureArray[0]() // 0 ☝️
  2. smartClosureArray[1]() // 1 ?
  3. smartClosureArray[2]() // 2 ?
  4. smartClosureArray[3]() // 3 ?
  5. smartClosureArray[4]() // 4 ?

Resources

Capture List in Closures

Source Code

3004_capture_lists.playground

Conclusion

You’ve learned the default behavior of a closure block which is identical to that of instances created with class objects. In fact, closures are reference types. In Chapter 5, however, you will discover some of problems that may arise due to the default behavior. Stay tuned.

In the following lesson, you will learn how to beautify closures.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.