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. This is lesson 2 of 3. 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 3 lessons in total.
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.
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 “Stored vs computed properties, observers” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Stored vs computed properties, observers”, 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 3 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
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)