0
0
Swiftprogramming~10 mins

Structs are value types (copy on assign) in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Structs are value types (copy on assign)
Create struct instance A
Assign A to B (copy)
Modify B
Check A and B values
A unchanged, B changed
When you assign a struct to a new variable, Swift copies the data. Changing the new variable does not affect the original.
Execution Sample
Swift
struct Point {
    var x: Int
    var y: Int
}

var a = Point(x: 1, y: 2)
var b = a
b.x = 10
This code creates a struct Point, copies it from a to b, then changes b.x. a remains unchanged.
Execution Table
StepActionVariableValue of aValue of bNote
1Create aaPoint(x:1, y:2)nila is initialized
2Assign a to b (copy)bPoint(x:1, y:2)Point(x:1, y:2)b is a copy of a
3Modify b.xbPoint(x:1, y:2)Point(x:10, y:2)b changed, a unchanged
4Check valuesa and bPoint(x:1, y:2)Point(x:10, y:2)a and b are independent
💡 Execution stops after verifying that modifying b does not affect a
Variable Tracker
VariableStartAfter 1After 2After 3Final
anilPoint(x:1, y:2)Point(x:1, y:2)Point(x:1, y:2)Point(x:1, y:2)
bnilnilPoint(x:1, y:2)Point(x:10, y:2)Point(x:10, y:2)
Key Moments - 2 Insights
Why does changing b.x not change a.x?
Because structs are value types, assigning a to b copies the data. So b is a separate copy, and changes to b do not affect a. See execution_table step 3.
Is b a reference to a or a separate copy?
b is a separate copy, not a reference. This is why modifying b does not change a. Refer to execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of b after step 2?
APoint(x:1, y:2)
BPoint(x:10, y:2)
Cnil
DPoint(x:0, y:0)
💡 Hint
Check the 'Value of b' column at step 2 in the execution_table.
At which step does b.x change to 10?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Value of b' columns in execution_table for when b.x changes.
If structs were reference types, what would happen when b.x changes?
Ab would become nil
Ba.x would remain 1
Ca.x would also change to 10
Da would be deleted
💡 Hint
Think about what happens when two variables point to the same data (reference type).
Concept Snapshot
structs are value types in Swift
Assigning one struct to another copies the data
Modifying the copy does not affect the original
Use structs for independent data copies
This behavior differs from classes (reference types)
Full Transcript
This visual trace shows how structs in Swift behave as value types. When you create a struct instance 'a' and assign it to 'b', Swift copies the data. Changing 'b' later does not affect 'a'. The execution table tracks each step: creating 'a', copying to 'b', modifying 'b', and checking both values. The variable tracker shows 'a' stays the same while 'b' changes. Key moments clarify why changes to 'b' don't affect 'a' because structs copy on assign. The quiz tests understanding of these steps and the difference from reference types. Remember, structs are for independent copies, unlike classes which share references.