willSet
willSet is a Swift language feature called a property observer. It runs immediately before a stored property’s value is updated, allowing code to react to the impending change. willSet applies to stored properties declared with var and is not invoked for constant properties (let) or for computed properties. It is also not called during the initial assignment of a property’s initial value.
Syntax and parameters: You declare a willSet block inside the property declaration. The block can name the
Behavior: willSet runs before the actual storage takes place, while didSet runs after the new value has
willSet {
print("Progress will be set to \(newValue)")
}
didSet {
print("Progress changed from \(oldValue) to \(progress)")
}
}
Common uses: logging changes, validating or reacting to impending updates, updating related state or UI, or