Complete the code to capture the variable x in the closure.
let x = 10 let closure = { [[1]] in print(x) } closure()
The capture list [x] captures the variable x inside the closure.
Complete the code to capture the variable count as a copy inside the closure.
var count = 5 let increment = { [[1] count] in print(count + 1) } count = 10 increment()
Using let count in the capture list captures a copy of count at the time the closure is created.
Fix the error by completing the capture list to avoid a strong reference cycle.
class Person { var name = "Alice" lazy var greet: () -> Void = { [[1] self] in print("Hello, \(self.name)!") } } let p = Person() p.greet()
Using [weak self] in the capture list prevents a strong reference cycle by capturing self weakly.
Fill both blanks to capture variables a and b as copies inside the closure.
var a = 1 var b = 2 let closure = { [[1] a, [2] b] in print(a + b) } a = 10 b = 20 closure()
Using let for both a and b captures copies of their values at closure creation.
Fill all three blanks to capture self weakly and variables x and y as copies.
class Calculator { var x = 3 var y = 4 lazy var compute: () -> Int = { [[1] self, [2] x, [3] y] in return x * y + (self?.x ?? 0) } } let calc = Calculator() print(calc.compute())
Capture self weakly to avoid retain cycles, and capture x and y as copies with let.