Skip to content
Justice Tech School

Properties

Stored properties hold values. Computed properties calculate. Observers react to changes.

Example
struct Rect {
    var w = 0.0, h = 0.0
    var area: Double { w * h }
    var label = "r" { didSet { print("changed to \(label)") } }
}
var r = Rect(w: 3, h: 4)
print(r.area)
r.label = "box"

Exercise

Calculated property.

var area: Double  w * h

Quiz Test

What is didSet?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.