0
0
Swiftprogramming~10 mins

Property observers (willSet, didSet) in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Property observers (willSet, didSet)
Property value about to change
willSet called
Property value changes
didSet called
Continue execution
When a property value changes, willSet runs before the change, and didSet runs after the change.
Execution Sample
Swift
var score: Int = 0 {
    willSet {
        print("About to set score to \(newValue)")
    }
    didSet {
        print("Score changed from \(oldValue) to \(score)")
    }
}
score = 10
This code shows how willSet and didSet run when the 'score' property changes from 0 to 10.
Execution Table
StepProperty 'score' ValueEventwillSet newValuedidSet oldValueOutput
10Initial state---
20 -> 10Assign score = 1010-About to set score to 10
310After assignment-0Score changed from 0 to 10
💡 Property 'score' changed from 0 to 10, observers finished.
Variable Tracker
VariableStartAfter willSetAfter didSetFinal
score00 (before change)10 (after change)10
Key Moments - 2 Insights
Why does willSet see the new value but didSet sees the old value?
willSet runs before the property changes, so it receives the newValue about to be set (see execution_table step 2). didSet runs after the change, so it sees the oldValue before the change (see execution_table step 3).
Can willSet or didSet change the property value inside their blocks?
No, changing the property inside willSet or didSet can cause unexpected behavior or infinite loops. They are meant only to observe changes, not modify them.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'score' during willSet?
A0 (old value)
B10 (new value)
CUndefined
DIt depends
💡 Hint
Check the 'Property 'score' Value' and 'willSet newValue' columns at step 2.
At which step does didSet run?
AStep 3
BStep 2
CStep 1
DAfter step 3
💡 Hint
Look at the 'Event' and 'Output' columns in the execution_table.
If we assign score = 20 next, what will willSet print?
A"Score changed from 10 to 20"
B"About to set score to 10"
C"About to set score to 20"
DNothing
💡 Hint
willSet prints the new value about to be assigned (see execution_table step 2).
Concept Snapshot
Property observers let you watch changes to a property.
willSet runs just before the value changes, receiving newValue.
didSet runs just after the value changes, receiving oldValue.
Use them to react to changes without changing the value inside.
Syntax: var prop: Type { willSet { } didSet { } }
Full Transcript
Property observers in Swift let you run code before and after a property changes. The willSet observer runs right before the property value changes and receives the new value that will be set. The didSet observer runs right after the property changes and receives the old value before the change. In the example, when 'score' changes from 0 to 10, willSet prints the new value 10 before the change, and didSet prints the old value 0 after the change. This helps you track or respond to changes safely. You should not change the property inside these observers to avoid unexpected behavior.