Introduction
A class in Swift is a reference type used to model entities that need shared, mutable state or identity — for example a network manager, a view controller, or an object that multiple parts of a program need to observe and mutate together. Unlike structs, classes support inheritance, letting one class build on and extend the behavior of another. Class instances are managed by Automatic Reference Counting (ARC), which deallocates an instance once nothing references it anymore.
Cricket analogy: A shared scoreboard object that every commentator's app references and updates in real time is like a Swift class — it has identity, and just as a franchise's core coaching staff can be inherited season to season, ARC keeps the scoreboard alive only as long as some broadcast feed still points to it.
Syntax
class Animal {
var name: String
init(name: String) {
self.name = name
}
func makeSound() {
print("...")
}
}Explanation
Unlike a struct, a class does not get a free memberwise initializer, so we must write our own init(name:) and assign the parameter to the stored property via self.name. Because classes are reference types, any variable holding an instance actually holds a reference to that instance's storage on the heap; copying the reference (via assignment or passing to a function) does not copy the underlying object.
Cricket analogy: Unlike a struct, a PlayerProfile class doesn't get a free memberwise initializer, so you write init(name:) and assign self.name = name yourself; handing a teammate a reference to that profile object doesn't clone the player, it just gives them another way to point at the same stats.
Example
class Animal {
var name: String
init(name: String) { self.name = name }
}
let a = Animal(name: "Rex")
let b = a // b references the SAME instance as a
b.name = "Buddy"
print("a.name = \(a.name)")
print("b.name = \(b.name)")Output
a.name = Buddy
b.name = BuddyKey Takeaways
- Classes are reference types — variables hold a reference to shared storage.
- Classes must define their own initializers; there is no automatic memberwise init.
- Classes support single inheritance, unlike structs.
- Instances are managed automatically by ARC (Automatic Reference Counting).
- Mutating an instance through one reference is visible through every other reference to it.
Practice what you learned
1. What kind of type is a Swift class?
2. What happens by default when you assign a class instance to a new variable?
3. Does Swift automatically generate a memberwise initializer for classes?
4. What manages the memory of class instances in Swift?
5. Which capability do classes have that structs do not?
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.
Structs vs Classes in Swift
Structs use value semantics while classes use reference semantics — the defining choice you make for every custom Swift type.
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