You can use the Willset and Didset monitors to declare a stored variable or property.
var num
= 0 {
Willset (setter name) {
Do something here
}
Didset ( setter name
) {
Do something here
}
}
monitor is not run when a variable or property is initialized for the first time, only if the value is changed externally.
Willset Monitor only runs before a variable or property value is changed. as a constant Willset monitor , It is therefore not possible to change it in the Willset statement. The Didset Monitor runs immediately after a variable or property value is changed . In contrast to the Willset monitor, in order to prevent you still need to get the old data, property Didset monitor . This means that if you set a value in the Didiset Monitor statement of the variable or property itself, the new value you set will replace the value that you just passed in the Willset Monitor.
In Willset and DIDSET statements, the statements for setter names and parentheses are optional. If you write a setter name, it will be used as a parameter for Willset and Didset. If you do not write the setter name, the Willset Monitor initially named Newvalue,didset Monitor initially named OldValue.
When you provide a willset statement, the Didset statement is optional. Similarly, when you provide a didset statement, the Willset statement is optional.
Storage-type variable monitor and property monitor Willset and Didset