0
0
Swiftprogramming~20 mins

Property observers (willSet, didSet) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Property Observer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
AScore will change from 10 to 0
BScore will change from 0 to 10
CScore will change from 0 to 0
DNo output
Attempts:
2 left
💡 Hint
Remember that willSet runs before the property value changes, and newValue holds the new value.
Predict Output
intermediate
2: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
ATemperature changed from 20 to 30
BTemperature changed from 30 to 20
CTemperature changed from 20 to 20
DNo output
Attempts:
2 left
💡 Hint
didSet runs after the property value changes, and oldValue holds the previous value.
🔧 Debug
advanced
2: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
AIt causes infinite recursion because didSet sets value again inside didSet.
BIt causes a syntax error because didSet cannot modify the property.
CIt causes a compile-time error because value is a constant.
DIt runs fine and sets value to 5 without error.
Attempts:
2 left
💡 Hint
Think about what happens when you assign to the property inside didSet.
🧠 Conceptual
advanced
2:00remaining
Which statement about willSet and didSet is true?
Choose the correct statement about Swift's property observers willSet and didSet:
AwillSet runs after the property value changes, and didSet runs before the change.
BwillSet provides the old value as newValue, and didSet provides the new value as oldValue.
CBoth willSet and didSet run only when the property is initialized.
DwillSet runs before the property value changes, and didSet runs after the change.
Attempts:
2 left
💡 Hint
Think about the timing of these observers relative to the property change.
Predict Output
expert
3: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
A
Inner willSet: 0 -> 6
Inner didSet: 0 -> 6
Outer willSet: 0 -> 3
Outer didSet: 0 -> 3
B
Outer willSet: 0 -> 3
Inner willSet: 0 -> 6
Outer didSet: 0 -> 3
Inner didSet: 0 -> 6
C
Outer willSet: 0 -> 3
Outer didSet: 0 -> 3
Inner willSet: 0 -> 6
Inner didSet: 0 -> 6
D
Outer didSet: 0 -> 3
Outer willSet: 0 -> 3
Inner didSet: 0 -> 6
Inner willSet: 0 -> 6
Attempts:
2 left
💡 Hint
Remember the order: willSet runs before the property changes, didSet after. Also, didSet of outer triggers inner.value change.