17.swift How to listen for changes to this property

Source: Internet
Author: User
Tags custom name

Import UIKit


class Person: NSObject {

var name: String? {

// can give newvalue a custom name

willset (new) { //The property is about to change and the method that is called when it is not changed

// In this method there is a default system attribute NewValue, which is used to store the new value

Print (name)

Print (new)

}

// can give oldValue a custom name

didset (old) { // property value has changed , the method that will be called

// In this method there is a default system attribute , OldValue, for storing old values

Print (name)

Print (old)

}

}

var age: Int = 0

var height: Double = 0.0

}


Let P: Person = person()


// when assigning , listen for changes to this property

// in OC by overriding the set method

// in Swift , You can add listeners to a property

P. Name = "Why"


P.name = "YZ"


17.swift How to listen for changes to this property

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.