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