When are property observers (`willSet` / `didSet`) *not* called in Swift?
iOS interview question for Intermediate practice.
Answer
Property observers (willSet and didSet) are specifically designed to react to value changes after an instance is fully initialized. They are therefore not called in the following situations: 1. During Initialization: When setting a property's default value as part of its definition (var name: String = "Default"). When setting a property's value within any initializer (init) of its defining type. 2. During willSet/didSet Itself (for the same property): Assigning back to the same property from within its own didSet block does not trigger the observers again (prevents infinite loops). You cannot assign back to the property from within willSet. 3. Lazy Property Initialization: Observers are not called for the initial computation and setting of a lazy var property when it's first accessed. They are primarily intended for responding to assignments made to the property outside of its own initialization process.
Explanation
Property observers are called when properties are set as part of subclass initialization, but only after the superclass initialization has completed (during Phase 2 of initialization).