Storage-type variable monitor and property monitor Willset and Didset

Source: Internet
Author: User

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

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.