Introduction to object-oriented programming
Learning intentions
In this lesson, you will build a practical foundation for object-oriented programming in Swift and use it to model real problems more clearly. Explain object-oriented programming in simple terms struct is
You will also connect these ideas to code quality so your programs are easier to read and maintain over time. Keep related data and behaviour together
What is object-oriented programming?
Why developers use object-oriented ideas
Object-oriented design helps teams grow codebases without losing clarity, because each model keeps related logic in one place. Related logic stays in one place
It also supports safer refactoring and collaboration, because clearer boundaries make it easier to track where a change belongs
Types and instances in Swift
In Swift, built-in types such as Int, String, and Array already follow this model-based style, and custom types work the same way. Int is a type let age = 18 creates an instance Player, Book, or Quest
When you define your own struct, you are creating a model for your own domain, so choose names that match the problem you are solving. Use names tied to the problem space
What is a struct?
A struct is a custom type that groups related properties and methods so one model can hold data and define behaviour together. Stored properties hold data
For beginner and intermediate Swift projects, struct is usually the best default because value semantics reduce accidental shared-state bugs
Anatomy of a struct
A useful struct has a clear name, meaningful properties, and behaviour that naturally belongs to the model itself. Keep property names specific
struct Player {
var name: String
var score: Int
func summary() -> String {
return "\(name) has \(score) points."
}
}
Creating instances
You create an instance by calling the type and passing values for its stored properties. Use Swift's memberwise initializer
let firstPlayer = Player(name: "Aria", score: 10)
let secondPlayer = Player(name: "Niko", score: 3)
Accessing properties and methods
After creating an instance, use dot syntax to read properties and call methods so your code can access both data and behaviour cleanly. player.name reads a property value player.summary() calls a method
print(firstPlayer.name) // Aria
print(firstPlayer.summary()) // Aria has 10 points.
Designing better structs
Strong structs model one concept well instead of trying to solve multiple unrelated problems at once. Keep each struct focused on one responsibility
This keeps your code easier to read, test, and change
Testing struct behaviour
When testing a struct, check both how data is stored and how methods behave so you can trust the model in different scenarios. Verify properties after initialization
let a = Player(name: "Tui", score: 0)
let b = Player(name: "Moana", score: 7)
print(a.summary()) // Tui has 0 points.
print(b.summary()) // Moana has 7 points.
Task A: race garage
- Create a struct called
Carwith propertiesbrand,model, andyear. - Create two
Carinstances and print a sentence about each car. - Use dot syntax to print each property value.
Task B: mini banking model
- Create a struct called
BankAccountwith propertiesownerandbalance. - Add a method named
description()that returns a sentence with both values. - Create two accounts and print
description()for each one.
Task C: area checker
- Create a struct called
Rectanglewith propertieswidthandheight. - Add a method named
area()that returns the area. - Create two rectangles
- Print both areas
- Determine which rectangle is the larger using some function or conditional statements.
Task D: quest board
- Create a struct called
Questwith propertiestitle,difficulty, andreward. - Add a method
printBadge()that prints a formatted line such as"Rescue the Prince - Hard Quest - 50 XP". - Create three quest instances of different difficulties.
- Call
printBadge()for each. - Determine which quest is hardest using some function or conditional statements.
Summary
Object-oriented programming helps you model software around meaningful entities so code is easier to organize and reason about. Types define the blueprint
Used consistently, these ideas improve readability, testing, and long-term maintainability
Review
Use these prompts to check your understanding and explain each idea in your own words. Explain object-oriented programming in one sentence struct is
