Computed properties with get/set
Create computed properties (read-only and read–write) using get/set, enforce invariants in setters, and use access control like private(set).
Computed properties with get/set is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Overview
Computed properties derive values on access and can synchronize state on set. Use them to keep invariants tidy.
Read-only computed
A read-only computed property omits set. Keep derivations simple and cheap.
struct Rectangle {
var width: Double
var height: Double
var area: Double { // read-only computed
width * height
}
}
let r = Rectangle(width: 3, height: 4)
print("area =", r.area) // 12Read–write get/set
Add get/set for a read–write computed property that keeps related values 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) // 7Setter validation
Setters can validate input. You may use the implicit newValue name when there's no custom name.
struct Percentage {
private var _raw: Int = 0 // backing store
var value: Int {
get { _raw }
set { // uses implicit newValue
_raw = min(100, max(0, newValue))
}
}
}
var p = Percentage()
p.value = 120
print(p.value) // 100
p.value = -5
print(p.value) // 0private(set)
Use private(set) to expose a value for reading while restricting writes to the type itself.
struct ScoreBoard {
private(set) var highScore = 0 // public read, internal write
mutating func update(_ v: Int) {
if v > highScore { highScore = v }
}
}
var s = ScoreBoard()
s.update(30)
print(s.highScore)
// s.highScore = 50 // error outside the typeNotes & tips
Notes:
- Computed properties don't store data; they compute on demand.
- They don't support willSet/didSet; put logic in set instead.
- Keep getters fast; heavy work belongs in methods.
Computed definition
Quick check: What defines a computed property?
Recap
Recap: Build read-only and read–write computed properties, validate in set, and use private(set) to control who can modify data.
Frequently asked questions
Is the “Computed properties with get/set” lesson free?
Yes — the full text of “Computed properties with get/set” is free to read here on the web, and the Swift Academy course includes 4 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 “Computed properties with get/set”?
Create computed properties (read-only and read–write) using get/set, enforce invariants in setters, and use access control like private(set). 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Computed properties with get/set” 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