Swift-Properties-Property monitor (Didset, Willset)

Source: Internet
Author: User

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)

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.