0
0
Swiftprogramming~10 mins

Reference sharing and side effects in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reference sharing and side effects
Create Object A
Assign Reference B = A
Modify Object via B
Check Object A's state
Observe Side Effects
End
This flow shows how two variables can point to the same object, so changing it through one affects the other.
Execution Sample
Swift
class Box {
    var value: Int
    init(value: Int) { self.value = value }
}

let boxA = Box(value: 10)
let boxB = boxA
boxB.value = 20
print(boxA.value)
This code creates a Box object, assigns it to two variables, changes the value via one, and prints the value via the other.
Execution Table
StepActionboxA.valueboxB.valueExplanation
1Create boxA with value 1010-boxA points to Box(value:10)
2Assign boxB = boxA1010boxB points to same Box as boxA
3Change boxB.value = 202020Both see updated value because same object
4Print boxA.value2020Output is 20, showing side effect
5End2020Execution stops
💡 Execution stops after printing boxA.value which is 20 due to shared reference
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
boxA.value-10102020
boxB.value--102020
Key Moments - 2 Insights
Why does changing boxB.value also change boxA.value?
Because boxB and boxA both point to the same Box object, changing the value through one reference affects the shared object, as shown in execution_table step 3.
Is boxB a copy of boxA or a reference to the same object?
boxB is a reference to the same object as boxA, not a copy. This is why changes via boxB affect boxA's value (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is boxA.value after step 3?
A20
B10
C0
DUndefined
💡 Hint
Check the 'boxA.value' column at step 3 in the execution_table.
At which step do boxA and boxB start pointing to the same object?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table where boxB is assigned.
If we changed boxB.value to 30 instead of 20 at step 3, what would boxA.value be after step 3?
A20
B30
C10
DUndefined
💡 Hint
Since boxA and boxB share the same object, changing boxB.value changes boxA.value too (see variable_tracker).
Concept Snapshot
Reference sharing means two variables point to the same object.
Changing the object via one variable affects the other.
In Swift, classes are reference types.
Use this to understand side effects.
Example: let b = a; b.value = x changes a.value too.
Full Transcript
This example shows how in Swift, when you assign one class instance to another variable, both variables refer to the same object in memory. Changing the object's property through one variable changes it for the other as well. The execution table traces creating a Box object with value 10, assigning it to boxB, changing boxB's value to 20, and printing boxA's value which is also 20. This demonstrates reference sharing and side effects clearly.