You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Structs and classes are the main building blocks for modelling data in Swift. This lesson covers their similarities, key differences, when to use each, and related concepts like properties, methods, and initialisers.
struct Point {
var x: Double
var y: Double
}
class Vehicle {
var make: String
var model: String
var year: Int
init(make: String, model: String, year: Int) {
self.make = make
self.model = model
self.year = year
}
}
// Structs get a free memberwise initialiser
var origin = Point(x: 0, y: 0)
// Classes require an explicit init
let car = Vehicle(make: "Tesla", model: "Model 3", year: 2024)
This is the most important distinction between structs and classes:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.