Introduction
Choosing between a struct and a class is one of the most important design decisions in Swift, and it is a favorite interview topic. The core difference is semantics: structs are value types that are copied on assignment, while classes are reference types where assignment shares the same underlying instance. Apple's official guidance is to prefer structs by default, and reach for a class only when you specifically need reference semantics, such as shared mutable state, identity, inheritance, or interoperability with Objective-C APIs.
Cricket analogy: Choosing struct vs class is like deciding whether a scorecard is a personal copy each fan takes home (value type, copied) or the single official scoreboard everyone shares and updates live (reference type); Apple says default to your own copy unless you need the shared scoreboard for team selection and captaincy identity.
Syntax
struct StructPoint { var x = 0, y = 0 }
class ClassPoint { var x = 0, y = 0 }Explanation
Even though StructPoint and ClassPoint look nearly identical, they behave very differently. Assigning a StructPoint to a new variable copies its x and y values, so the two variables are fully independent afterward. Assigning a ClassPoint copies only the reference, so both variables point to the same object in memory, and mutating one is visible through the other. Structs also cannot be subclassed or extended via inheritance, whereas classes can form inheritance hierarchies.
Cricket analogy: Assigning a StructPoint is like handing a friend a photocopy of your scorecard — they can scribble on it and your original stays clean; assigning a ClassPoint is like both of you looking at the same live scoreboard — one person's correction changes what the other sees too, and structs can't be subclassed like a bowling action can't be inherited.
Example
struct StructPoint { var x = 0 }
class ClassPoint { var x = 0 }
var s1 = StructPoint(); var s2 = s1
s2.x = 10
let c1 = ClassPoint(); let c2 = c1
c2.x = 10
print("s1.x = \(s1.x), s2.x = \(s2.x)")
print("c1.x = \(c1.x), c2.x = \(c2.x)")Output
s1.x = 0, s2.x = 10
c1.x = 10, c2.x = 10Key Takeaways
- Structs use value semantics; classes use reference semantics.
- Copies of a struct are independent; copies of a class reference are not.
- Apple recommends preferring structs unless you specifically need class features.
- Only classes support inheritance and Objective-C interoperability.
- Classes are useful when you need shared, mutable identity across a program.
- String, Array, and Dictionary are structs, showing structs are the norm, not the exception, in Swift.
Practice what you learned
1. What is the fundamental difference between structs and classes in Swift?
2. According to Apple's guidance, which type should you prefer by default?
3. Which feature is exclusive to classes?
4. If you copy a struct and then mutate the copy, what happens to the original?
5. Which operator checks whether two class references point to the exact same instance?
Was this page helpful?
You May Also Like
Structs in Swift
Structs are value types in Swift that are copied on assignment and come with a free memberwise initializer.
Classes in Swift
Classes are reference types in Swift that support inheritance and are managed automatically via ARC.
Inheritance in Swift
Inheritance lets a class build on a superclass's properties and methods, overriding behavior with the override keyword.
Memory Management and ARC in Swift
Understand how Automatic Reference Counting manages class instance memory in Swift and how to avoid retain cycles.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics