Property Monitor
1. Property Monitor Function
Property Observer control and response property changes , each time the property is set to the value of the Call Property observer , even if the new value and the old value of the same time is no exception.
Property observers can be added by overloading the property's methods for who can inherit ( including storage properties and computed properties )
Note: You do not need to add a property observer for computed properties that cannot be overloaded , because you can directly monitor and respond to changes in values by setter
2. Property Monitor Method
1. Willset
Features: called before a new value is set
Description: The Illset observer will Pass in the new attribute value as a fixed parameter, in Willset A name for this parameter can be specified in the Item code , and If not specified, the default name of the parameter is represented by NewValue
2.Didset
function: called immediately after the new value has been set
Gong can: The dIDSet Observer passes the old property values as parameters , with the default name of the parameter : OldValue
3. Attention
1. Willset and Didset observers are not called during property initialization , they are called only when the value of the property is set outside of the initialization
2. If you assign a value to a property in the Didset Observer , this value replaces the value set by the Observer.
4. code example
Class Person:nsobject {//Age attribute var age:int?//name attribute var name:string? {willset{println ("Willset")}didset{println ("Didset")}}}
Swift-Properties-Property monitor (Didset, Willset)