Complete the code to declare a property observer that runs before the value changes.
var score: Int = 0 { [1] { print("Score will change to \(newValue)") } }
The willSet observer runs just before the property value changes.
Complete the code to print the old value after the property changes.
var level: Int = 1 { didSet { print("Level changed from \([1]) to \(level)") } }
The didSet observer runs after the value changes and provides oldValue to access the previous value.
Fix the error in the property observer to correctly access the new value.
var health: Int = 100 { willSet { print("Health will be set to \([1])") } }
Inside willSet, newValue holds the value about to be set.
Fill both blanks to create a property observer that prints old and new values.
var temperature: Double = 20.0 { willSet { print("Temperature will change from \([1]) to \([2])") } }
In willSet, the current value is accessed by the property name, and the new value is newValue.
Fill all three blanks to create a didSet observer that prints old and new values and updates a label.
var progress: Int = 0 { didSet { print("Progress changed from \([1]) to \([2])") progressLabel.text = "Progress: \([3])%" } }
In didSet, oldValue is the previous value, and the property name progress holds the new value.