Lesson 5 • Intermediate
Object-Oriented Programming 🏗️
Master structs vs classes, inheritance, protocols (Swift's version of interfaces), and enums with associated values — the building blocks of every Swift application.
What You'll Learn in This Lesson
- • Structs (value types) vs Classes (reference types) — and when to use each
- • Properties: stored, computed, and property observers
- • Inheritance, method overriding, and the
finalkeyword - • Protocols and protocol-oriented programming
- • Enums with associated values, raw values, and methods
1️⃣ Structs vs Classes
Swift has both structs and classes, but unlike most languages, structs are the default choice. Apple's own frameworks (SwiftUI, most of the standard library) are built heavily on structs. Classes are used when you need inheritance or reference semantics.
Try It: Structs vs Classes
Value types, reference types, properties, and computed properties
// Structs vs Classes in Swift
console.log("=== Structs (Value Types) ===");
console.log("Structs are COPIED when assigned or passed.");
console.log();
// Simulating a struct (value type)
function createPoint(x, y) {
return { x: x, y: y, toString() { return "(" + this.x + ", " + this.y + ")"; } };
}
let pointA = createPoint(3, 5);
let pointB = Object.assign({}, pointA); // copy (struct behavior)
pointB.x = 10;
console.log("pointA = " + pointA.toString() + " // unchanged!");
console.log("poi
...2️⃣ Inheritance & Protocols
Classes support single inheritance. Swift's protocols (similar to interfaces) define a contract that any type can conform to — structs, classes, and even enums. This enables Swift's protocol-oriented programming paradigm.
Try It: Inheritance & Protocols
Class hierarchies, method overriding, and protocol conformance
// Inheritance and Protocols in Swift
console.log("=== Inheritance ===");
class Animal {
constructor(name, sound) { this.name = name; this.sound = sound; }
speak() { return this.name + " says " + this.sound; }
describe() { return "Animal: " + this.name; }
}
class Dog extends Animal {
constructor(name, breed) {
super(name, "Woof!");
this.breed = breed;
}
// Override
describe() { return "Dog: " + this.name + " (" + this.breed + ")"; }
fetch(item) { return this.name + " fe
...3️⃣ Enums with Superpowers
Swift enums are incredibly powerful compared to other languages. They can have associated values (each case carries different data), raw values, computed properties, and methods. They're used extensively for modeling state machines and API results.
Try It: Enums
Associated values, raw values, and enum methods
// Swift Enums — Far More Powerful Than You Think
console.log("=== Basic Enums ===");
const Direction = { north: "north", south: "south", east: "east", west: "west" };
let heading = Direction.north;
console.log("var heading: Direction = ." + heading);
console.log();
// Enums with associated values (Swift's killer feature!)
console.log("=== Enums with Associated Values ===");
class NetworkResult {
constructor(type, data) { this.type = type; this.data = data; }
}
function processResult(result
...⚠️ Common Mistakes
mutating keyword.enum Result<Success, Failure> for any operation that can succeed or fail — it's built into Swift!📋 Quick Reference — OOP
| Concept | Syntax |
|---|---|
| Struct | struct Point { var x: Int } |
| Class | class Dog: Animal { ... } |
| Protocol | protocol Drawable { func draw() } |
| Enum | enum Result { case success(Data) } |
| Override | override func speak() |
| Computed prop | var area: Double { return w*h } |
🎉 Lesson Complete!
You've mastered OOP in Swift! Next, learn to build beautiful user interfaces with SwiftUI.
Sign up for free to track which lessons you've completed and get learning reminders.