0
0
Swiftprogramming~10 mins

Capturing values from context in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Capturing values from context
Define variable x = 10
Define closure capturing x
Call closure
Closure uses captured x
Print result
Change x after closure defined
Call closure again
Closure uses current value of captured x
This flow shows how a closure captures a variable by reference from its surrounding context and uses the current value of the captured variable when called, even after the original variable changes later.
Execution Sample
Swift
var x = 10
let closure = { print("x is \(x)") }
closure()
x = 20
closure()
This code defines a variable x, a closure capturing x, calls the closure, changes x, then calls the closure again to show captured value behavior.
Execution Table
StepActionVariable xClosure Output
1Define x = 1010
2Define closure capturing x10
3Call closure first time10x is 10
4Change x to 2020
5Call closure second time20x is 20
💡 Program ends after second closure call showing updated x value
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5
x10102020
Key Moments - 2 Insights
Does the closure keep the original value of x or the updated value after x changes?
In Swift, closures capture variables by reference, so they see the updated value of x when called, as shown in steps 3 and 5 of the execution_table.
Why does the closure print "x is 20" after x changes to 20?
Because the closure captures the variable x itself, not just its value at definition, so it reflects the current value of x when called (see step 5 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed when the closure is called the first time?
A"x is 0"
B"x is 10"
C"x is 20"
DNo output
💡 Hint
Check step 3 in the execution_table where the closure is called first time.
At which step does the variable x change its value?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the variable_tracker and execution_table rows for when x changes.
If x was a constant (let x = 10), what would happen when trying to change x at step 4?
AThe program would compile and run normally
BThe closure would capture the value 10 and never change
CThere would be a compile-time error at step 4
DThe closure would print "x is 20" anyway
💡 Hint
In Swift, constants cannot be changed after definition; check step 4 where x is assigned 20.
Concept Snapshot
Capturing values from context in Swift:
- Closures capture variables by reference.
- They use the current value of captured variables when called.
- Changing a variable after closure definition affects closure output.
- Constants (let) cannot be changed after definition.
- Useful for delayed execution with access to surrounding variables.
Full Transcript
This example shows how Swift closures capture variables from their surrounding context. We start by defining a variable x with value 10. Then we define a closure that prints x. When we call the closure the first time, it prints "x is 10". After that, we change x to 20. Calling the closure again prints "x is 20" because closures capture variables by reference, not by value. This means the closure sees the current value of x when it runs. If x were a constant, changing it would cause a compile error. This behavior helps closures access and use variables from their context even after that context changes.