0
0
Swiftprogramming~10 mins

Closures are reference types in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Closures are reference types
Create closure instance
Assign closure to variable A
Assign variable A to variable B
Modify closure via B
Observe change via A
Both A and B refer to same closure instance
This flow shows how closures are created and assigned to variables, and how multiple variables can reference the same closure instance, demonstrating closures are reference types.
Execution Sample
Swift
var closureA = { () -> Int in
    var count = 0
    return { count += 1; return count }
}()

var closureB = closureA
closureB()
closureA()
This code creates a closure that increments a count, assigns it to two variables, and calls it via both to show shared state.
Execution Table
StepActionVariable StatesOutputNotes
1Create closureA with internal count=0closureA: closure with count=0-Closure instance created with count=0
2Assign closureA to closureBclosureA: closure with count=0, closureB: same closure-closureB references same closure instance as closureA
3Call closureB()count increments to 11Closure called via closureB, count becomes 1
4Call closureA()count increments to 22Closure called via closureA, count becomes 2
5EndclosureA and closureB share count=2-Both variables reference same closure instance
💡 Execution ends after demonstrating shared state through closure references
Variable Tracker
VariableStartAfter Step 3After Step 4Final
closureA.count0122
closureB.count0122
Key Moments - 2 Insights
Why does calling closureA() after closureB() show the incremented count?
Because closureA and closureB reference the same closure instance, changes via closureB affect closureA as shown in steps 3 and 4 of the execution_table.
Is a new closure created when assigning closureA to closureB?
No, closureB points to the same closure instance as closureA, so no new closure is created (see step 2 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of count inside the closure after calling closureB()?
A2
B0
C1
DUndefined
💡 Hint
Check the 'Variable States' column at step 3 in execution_table.
At which step does closureA and closureB share the same updated count value?
AStep 4
BStep 2
CStep 1
DStep 5
💡 Hint
Look at the 'Variable States' and 'Notes' columns at step 4 in execution_table.
If closureB was assigned a new closure instead of closureA, how would the variable_tracker change?
AclosureA.count and closureB.count would increment together
BclosureB.count would stay at 0 after calls
CclosureA.count would reset to 0 after step 3
DclosureB.count would be undefined
💡 Hint
Consider that separate closures have independent internal states, unlike shared references shown in variable_tracker.
Concept Snapshot
Closures in Swift are reference types.
Assigning a closure to another variable copies the reference, not the closure itself.
Calling the closure via any reference affects the same internal state.
This means closures share captured variables when referenced.
Use this to maintain shared state across calls.
Full Transcript
This visual execution trace shows how closures in Swift behave as reference types. We start by creating a closure that captures a variable count initialized to zero. This closure is assigned to closureA. Then closureA is assigned to closureB, so both variables point to the same closure instance. When closureB is called, it increments count to 1. Calling closureA next increments count to 2, showing that both variables share the same closure and internal state. The variable tracker confirms count changes are shared. Key moments clarify that no new closure is created on assignment and that both variables reference the same closure instance. The quiz tests understanding of closure state sharing and reference behavior. This demonstrates closures keep shared state because they are reference types in Swift.