Tsallaka zuwa abun ciki
Justice Tech School

Swift Properties

Stored properties hold values, computed properties calculate them with get and set, and property observers willSet and didSet react to changes. lazy delays creation.

Example
struct Circle {
    var r: Double
    var area: Double { 3.14 * r * r }
    var label = "" { didSet { print("label is now \(label)") } }
}
var c = Circle(r: 2)
print(c.area)
c.label = "big"

Exercise

Run after a change.

var x = 0 {  { } }

Quiz Test

What is a computed property?

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