Challenge - 5 Problems
Swift Property Observer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Swift code using willSet?
Consider this Swift code with a property observer
willSet. What will be printed when score is set to 10?Swift
class Game { var score: Int = 0 { willSet { print("Score will change from \(score) to \(newValue)") } } } let game = Game() game.score = 10
Attempts:
2 left
💡 Hint
Remember that willSet runs before the property value changes, and newValue holds the new value.
✗ Incorrect
The willSet observer runs just before the property changes. The old value is still accessible via the property itself (score), and the new value is available as newValue. So it prints the old value 0 and the new value 10.
❓ Predict Output
intermediate2:00remaining
What is printed by this Swift code using didSet?
Look at this Swift code with a
didSet observer. What will be printed after setting temperature to 30?Swift
struct Weather { var temperature: Int = 20 { didSet { print("Temperature changed from \(oldValue) to \(temperature)") } } } var today = Weather() today.temperature = 30
Attempts:
2 left
💡 Hint
didSet runs after the property value changes, and oldValue holds the previous value.
✗ Incorrect
The didSet observer runs after the property changes. The oldValue is 20, and the new value is 30, so it prints the change correctly.
🔧 Debug
advanced2:30remaining
Why does this Swift code cause a runtime error?
This Swift code uses didSet to update the property. Why does it crash when setting
value to 5?Swift
class Counter { var value: Int = 0 { didSet { value = 10 } } } let counter = Counter() counter.value = 5
Attempts:
2 left
💡 Hint
Think about what happens when you assign to the property inside didSet.
✗ Incorrect
Assigning to the property inside didSet triggers didSet again, causing infinite recursion and a runtime crash.
🧠 Conceptual
advanced2:00remaining
Which statement about willSet and didSet is true?
Choose the correct statement about Swift's property observers
willSet and didSet:Attempts:
2 left
💡 Hint
Think about the timing of these observers relative to the property change.
✗ Incorrect
willSet runs just before the property changes, giving access to the new value as newValue. didSet runs just after the property changes, giving access to the old value as oldValue.
❓ Predict Output
expert3:00remaining
What is the final output of this Swift code with nested property observers?
Analyze this Swift code with two properties having observers. What will be printed after setting
outer.value to 3?Swift
class Inner { var value: Int = 0 { willSet { print("Inner willSet: \(value) -> \(newValue)") } didSet { print("Inner didSet: \(oldValue) -> \(value)") } } } class Outer { var value: Int = 0 { willSet { print("Outer willSet: \(value) -> \(newValue)") } didSet { print("Outer didSet: \(oldValue) -> \(value)") inner.value = value * 2 } } var inner = Inner() } let outer = Outer() outer.value = 3
Attempts:
2 left
💡 Hint
Remember the order: willSet runs before the property changes, didSet after. Also, didSet of outer triggers inner.value change.
✗ Incorrect
First, outer.value's willSet runs printing the old and new values. Then outer.value changes, triggering outer.didSet, which prints old and new values and sets inner.value to 6. Setting inner.value triggers inner's willSet and didSet in order.