Stored vs computed properties, observers
Define stored vs computed properties, create read-only and get/set computed properties, and react to changes with willSet/didSet observers.
Stored vs computed properties, observers is a free Swift Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Overview
This lesson contrasts stored vs computed properties and shows willSet/didSet observers to react to changes.
Stored vs computed
Stored properties keep data. Computed properties calculate a value when accessed.
struct Rectangle {
// stored properties (hold data)
var width: Double
var height: Double
// computed property (derived on access)
var area: Double {
return width * height // read-only computed
}
}
let r = Rectangle(width: 3, height: 4)
print("area =", r.area) // 12Computed get/set
Add get/set to make a read–write computed property that keeps invariants in sync.
struct Circle {
var radius: Double // stored
var diameter: Double { // computed read-write
get { radius * 2 }
set { radius = newValue / 2 }
}
}
var c = Circle(radius: 5)
print(c.diameter) // 10
c.diameter = 14
print(c.radius) // 7Observers in action
Observers run around changes to a stored property. Use newValue/oldValue for context.
struct Counter {
var value: Int = 0 {
willSet { print("will set to", newValue) }
didSet { print("did set from", oldValue) }
}
}
var cnt = Counter()
cnt.value = 5
cnt.value = 9Observers vs computed
Notes:
- Observers apply to stored properties (including ones you set in extensions).
- Computed properties do not support observers—put logic in set instead.
- Observers are not called during initialization before storage is ready.
Practical pattern
Combine observers to normalize input and a computed property to derive display data.
struct Score {
var raw: Int = 0 {
didSet {
if raw < 0 { raw = 0 }
if raw > 100 { raw = 100 }
}
}
var label: String {
// computed: derive a label from current raw
if raw >= 90 { return "A" }
if raw >= 80 { return "B" }
if raw >= 70 { return "C" }
return "D"
}
}
var s = Score()
s.raw = 120
print(s.raw, s.label) // 100 A
s.raw = -5
print(s.raw, s.label) // 0 DObserver rule
Quick check: What runs before/after a stored property changes?
Recap
Recap: Use stored properties for data, computed for derived values (get/set), and willSet/didSet to react to changes on stored properties.
Frequently asked questions
Is the “Stored vs computed properties, observers” lesson free?
Yes — the full text of “Stored vs computed properties, observers” is free to read here on the web, and the Swift Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Stored vs computed properties, observers”?
Define stored vs computed properties, create read-only and get/set computed properties, and react to changes with willSet/didSet observers. 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; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Stored vs computed properties, observers” 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
- Structs vs Classes, properties & inits
- Stored vs computed properties, observers
- Initializers & deinit (classes)