Predict the output
What does this print?
var count = 0
let a = { print(count) }
let b = { [count] in print(count) }
count = 5
a()
b()Hint 1 of 1
When is a capture list evaluated?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B 5 then 0
Correct. a reads the live variable; b copied 0 when it was created.
Why not the others?
- A 0 then 0.
acaptures the variable itself, so it sees the change to 5. - C 5 then 5. The capture list in
btook a snapshot before the change. - D It doesn't compile. Both closures are valid.