Swift-Attribute Viewer (Willset and Didset)

Source: Internet
Author: User

Property observers, similar to triggers. A property value change that is used to monitor properties except for initialization, which can be responded to when the value of the property has changed. Has the following characteristics:
1, not only can trigger didset after the property value changes, but also can trigger willset before the property value changes.
2, adding an observer to an attribute must declare a clear attribute type, or the compiler will give an error.
3,willset can take a newname parameter, if not, the parameter is named NewValue by default.
4,didset can take a oldname parameter, which represents the old attribute, and the default name is OldValue.
5, Willset and Didset are not called when the property is initialized. It is called only if the property value is set outside of the initialization context.
6, even if the value set is the same as the original value, Willset and Didset are also called

Examples are as follows:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 class People{    //普通属性    var firstName:String = ""    var lastName:String= ""    var nickName:String= ""        //计算属性    var fullName:String    {        get        {            return nickName + " " + firstName + " " + lastName        }    }        //带属性监视器的普通属性    var age:Int = 0    {        //我们需要在age属性变化前做点什么        willSet        {            println("Will set an new value \(newValue) to age")        }        //我们需要在age属性发生变化后,更新一下nickName这个属性        didSet        {            println("age filed changed form \(oldValue) to \(age)")            if age<10            {                nickName = "Little"            }else            {                nickName = "Big"            }        }    }         func toString() -> String    {        return "Full Name: \(fullName) " + ", Age: \(age) "    }    } var me = People()me.firstName = "Zhang"me.lastName  = "San"me.age = 20println(me.toString())/*程序输出Will set an new value 20 to ageage filed changed form 0 to 20Full Name: Big Zhang San , Age: 20*/

Swift-Attribute Viewer (Willset and Didset)

Related Article

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.