Properties & Subscripts
Use lazy stored properties, get/set computed properties, property observers, and subscripts for custom indexing.
Properties & Subscripts is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
Intro
This lesson covers advanced properties: lazy, computed with get/set, observers, plus custom subscripts.
lazy property
lazy properties are created only on first access. Useful for expensive work.
struct DataLoader {
lazy var data: [Int] = {
print("Loading...")
return [1,2,3]
}()
}
var loader = DataLoader()
print("before access")
print(loader.data)Computed get/set
Computed properties use get/set blocks to calculate or update values.
struct Circle {
var radius: Double
var diameter: Double {
get { return radius * 2 }
set { radius = newValue / 2 }
}
}
var c = Circle(radius: 5)
print(c.diameter) // 10
c.diameter = 20
print(c.radius) // 10Observers
Use willSet and didSet to react to property changes.
class Account {
var balance: Double = 0 {
willSet { print("Will set to", newValue) }
didSet { print("Did set from", oldValue) }
}
}
var a = Account()
a.balance = 50Subscript
Subscripts let you define custom access with bracket syntax.
struct TimesTable {
let multiplier: Int
subscript(i: Int) -> Int {
return multiplier * i
}
}
let three = TimesTable(multiplier: 3)
print(three[4]) // 12Multi-parameter subscript
Subscripts can take multiple parameters, e.g., a matrix with row and column indices.
struct Matrix {
var grid: [[Int]]
subscript(row: Int, col: Int) -> Int {
return grid[row][col]
}
}
let m = Matrix(grid:[[1,2],[3,4]])
print(m[1,0]) // 3lazy Q
Quick check: What does the lazy keyword do?
Recap
Recap: Use lazy for deferred init, get/set for computed props, willSet/didSet for observing, and subscripts for custom access.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Properties & Subscripts” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Properties & Subscripts”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “Properties & Subscripts”?
Use lazy stored properties, get/set computed properties, property observers, and subscripts for custom indexing. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Properties & Subscripts” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Properties & Subscripts
- lazy & property observers
- Computed properties with get/set
- Subscripts for custom indexing