How to use "Property monitor"

Source: Internet
Author: User
Tags custom name

Property Monitor

The property monitor monitors and responds to changes in the property value. It calls the property monitor every time the property is set. Even when the new value is the same as the current value, it is no exception.

 

You can add a property monitor for other storage properties except for Delayed Storage Properties, or you can add property monitors for inherited properties (including storage and computing properties) by reloading properties. For more information about property overloading, see the inheritance section.

 

Note:

 

You do not need to add a property monitor for computing properties that cannot be reloaded, because you can directly monitor and respond to value changes through setter.

You can add one or all of the following monitors to the property:

 

Willset is called before the new value is set.

Didset is called immediately after the new value is set.

Willset monitor will pass in the new property value as a fixed parameter. In the implementation code of willset, you can specify a name for this parameter. If this parameter is not specified, the parameter is still available, and the default name newvalue is used.

 

Similarly, the didset monitor will pass in the old property value as a parameter. You can name this parameter or use the default parameter name oldvalue.

 

Note:

 

Willset and didset monitors will not be called during attribute initialization. They will only be called when attribute values are set outside of initialization.

Here is an actual example of willset and didset. It defines a class named stepcounter to count the total number of steps when a person walks, it can be used with the input data of the pedometer or other statistical devices for daily exercise.

 

class StepCounter {   var totalSteps: Int = 0 {   willSet(newTotalSteps) {       println("About to set totalSteps to \(newTotalSteps)")    }   didSet {       if totalSteps > oldValue  {           println("Added \(totalSteps - oldValue) steps")       }    }    }}let stepCounter = StepCounter()stepCounter.totalSteps = 200// About to set totalSteps to 200// Added 200 stepsstepCounter.totalSteps = 360// About to set totalSteps to 360// Added 160 stepsstepCounter.totalSteps = 896// About to set totalSteps to 896// Added 536 steps


The stepcounter class defines a totalsteps attribute of the int type, which is a storage attribute, including willset and didset monitor.

 

When totalsteps sets a new value, both willset and didset monitors of totalsteps will be called, even if the new value is the same as the current value.

 

In this example, the willset monitor defines the new value as newtotalsteps, which simply outputs the new value.

 

The didset monitor is called after the value of totalsteps changes. It compares the new value with the old value. If the total number of steps increases, a message is output to indicate the number of steps added. Didset does not provide a custom name. Therefore, the default value oldvalue indicates the parameter name of the old value.

 

Note:

 

If a value is assigned to a property in the didset monitor, this value replaces the value set before the monitor.

How to use "Property monitor"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.