0
0
Swiftprogramming~10 mins

Why structs are preferred in Swift - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why structs are preferred in Swift
Define Struct
Create Instance
Copy Instance
Modify Copy
Original Unchanged
Safe, Fast, Predictable
Structs in Swift are value types that get copied when assigned or passed, making them safe and predictable by avoiding shared state.
Execution Sample
Swift
struct Point {
  var x: Int
  var y: Int
}

var p1 = Point(x: 1, y: 2)
var p2 = p1
p2.x = 10
This code shows creating a struct instance, copying it, and modifying the copy without changing the original.
Execution Table
StepActionVariable StateExplanation
1Define struct Point with x and yNo instances yetStruct type is ready to use
2Create p1 = Point(x:1, y:2)p1: (x=1, y=2)p1 holds a Point with x=1, y=2
3Assign p2 = p1 (copy)p1: (x=1, y=2), p2: (x=1, y=2)p2 is a copy of p1, separate memory
4Modify p2.x = 10p1: (x=1, y=2), p2: (x=10, y=2)Changing p2 does not affect p1
5Check p1.xp1.x = 1Original p1 remains unchanged
6Check p2.xp2.x = 10Modified copy reflects new value
💡 Execution stops after demonstrating that modifying a struct copy does not change the original instance.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
p1undefined(x=1, y=2)(x=1, y=2)(x=1, y=2)(x=1, y=2)
p2undefinedundefined(x=1, y=2)(x=10, y=2)(x=10, y=2)
Key Moments - 3 Insights
Why does changing p2.x not affect p1.x?
Because structs are value types, assigning p1 to p2 copies the data. So p2 is independent, as shown in step 4 and 5 of the execution_table.
What happens when we assign one struct variable to another?
A full copy of the struct is made, not a reference. This is why p2 gets its own copy in step 3.
Why are structs considered safer than classes in Swift?
Structs avoid shared mutable state because each copy is independent, preventing unexpected changes. This is demonstrated by p1 and p2 having different values after modification.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of p1.x?
A10
B1
CUndefined
D0
💡 Hint
Check the Variable State column at step 4 where p1.x remains unchanged.
At which step does p2 get its own copy of p1?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the Action column where p2 is assigned from p1.
If structs were reference types, what would happen when p2.x is changed?
Ap1.x would stay the same
Bp2.x would revert to original
Cp1.x would also change
Dp2.x would become undefined
💡 Hint
Think about shared references vs copies shown in variable_tracker.
Concept Snapshot
Swift structs are value types.
Assigning a struct copies its data.
Modifying a copy does not affect the original.
This makes structs safe and predictable.
Preferred for simple data models and thread safety.
Full Transcript
This visual execution shows why Swift prefers structs. Structs are value types, meaning when you assign one struct variable to another, it copies the data instead of sharing it. For example, when we create p1 as a Point struct and then assign p2 = p1, p2 gets its own copy. Changing p2.x does not change p1.x. This behavior avoids bugs from shared state and makes code safer and easier to understand. The execution table traces each step, showing variable states and actions. Key moments clarify common confusions about copying and independence of structs. The quiz tests understanding of these concepts by referencing the execution steps and variable changes.