0
0
Swiftprogramming~10 mins

Comparing structs vs classes decision in Swift - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Comparing structs vs classes decision
Start: Need to model data
Decide: Value or Reference?
Use Struct
Copy on [Shared reference
changes affect all
Immutable [Mutable by default
End Decision
This flow shows how to decide between struct and class by choosing value or reference type, considering copying behavior, mutability, and use cases.
Execution Sample
Swift
struct Point {
  var x: Int
  var y: Int
}

class Circle {
  var center: Point
  var radius: Int
  init(center: Point, radius: Int) {
    self.center = center
    self.radius = radius
  }
}
Defines a struct Point as a value type and a class Circle as a reference type holding a Point.
Execution Table
StepActionVariable/InstanceValue/StateNotes
1Create Point p1p1Point(x: 1, y: 2)Struct instance created
2Assign p2 = p1p2Point(x: 1, y: 2)Copy of p1, independent
3Modify p2.x = 3p2Point(x: 3, y: 2)p2 changed, p1 unchanged
4Create Circle c1c1Circle(center: Point(x: 1, y: 2), radius: 5)Class instance created
5Assign c2 = c1c2Circle(center: Point(x: 1, y: 2), radius: 5)Reference copy, same instance
6Modify c2.radius = 10c2Circle(center: Point(x: 1, y: 2), radius: 10)Change affects c1 too
7Check c1.radiusc1.radius10Shows shared reference
8EndDemonstrates struct copy vs class reference
💡 Execution stops after showing difference in copying behavior between struct and class.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
p1undefinedPoint(x:1,y:2)Point(x:1,y:2)Point(x:1,y:2)Point(x:1,y:2)Point(x:1,y:2)
p2undefinedPoint(x:1,y:2)Point(x:3,y:2)Point(x:3,y:2)Point(x:3,y:2)Point(x:3,y:2)
c1undefinedundefinedundefinedCircle(center:Point(x:1,y:2),radius:5)Circle(center:Point(x:1,y:2),radius:10)Circle(center:Point(x:1,y:2),radius:10)
c2undefinedundefinedundefinedCircle(center:Point(x:1,y:2),radius:5)Circle(center:Point(x:1,y:2),radius:10)Circle(center:Point(x:1,y:2),radius:10)
Key Moments - 3 Insights
Why does changing p2 not affect p1, but changing c2 affects c1?
Because p1 and p2 are structs (value types), assigning p2 = p1 copies the data, so they are independent (see steps 2 and 3). c1 and c2 are classes (reference types), so c2 = c1 copies the reference, both point to the same object (see steps 5 and 6).
What does it mean that structs are 'immutable by default'?
Structs are value types and if declared with 'let', their properties cannot change. In this example, we used 'var' so properties can change, but copying still creates independent copies (see step 3).
When should I choose a class over a struct?
Choose a class when you need shared mutable state or inheritance. Classes allow multiple references to the same object, so changes affect all references (see steps 5-7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of p1 after step 3?
APoint(x: 3, y: 2)
BPoint(x: 1, y: 3)
CPoint(x: 1, y: 2)
DUndefined
💡 Hint
Check the 'Value/State' column for p1 at step 3 in the execution table.
At which step does changing one variable affect another due to shared reference?
AStep 3
BStep 6
CStep 2
DStep 1
💡 Hint
Look at steps where c1 and c2 share the same instance in the execution table.
If p2 was a class instead of a struct, what would happen when modifying p2.x at step 3?
Ap1.x would also change
Bp1.x would stay the same
CCompilation error
Dp2.x would revert to original
💡 Hint
Recall that classes are reference types and changes affect all references (see steps 5-7).
Concept Snapshot
Structs are value types: copied on assignment, independent, good for simple data.
Classes are reference types: shared references, changes affect all, support inheritance.
Use structs for safety and simplicity; use classes for shared mutable state or polymorphism.
Structs are immutable if declared with let; classes are mutable by default.
Decide based on whether you want copies or shared objects.
Full Transcript
This visual execution compares structs and classes in Swift. Structs are value types, so when you assign one struct to another, it copies the data. Changing the copy does not affect the original. Classes are reference types, so assigning one class instance to another copies the reference, not the data. Changing one reference affects the other because they point to the same object. The example shows creating a Point struct and copying it, then modifying the copy without changing the original. It also shows creating a Circle class, copying the reference, and modifying one reference changes the shared object. This helps decide when to use structs or classes based on copying behavior, mutability, and use cases.