Property Observers
You can use Observers properties in Swift to accomplish what you need ... Here's what the docs say about them:
Property observers observe and respond to changes in value properties. Property observers are called every time the value of the set property, even if the new value matches the current properties of the value.
There are observers for the willSet and didSet :
To solve your problem, you can do something like this:
var myProperty:Int = 0 { willSet { print("About to set myProperty, newValue = \(newValue)") } didSet{ print("myProperty is now \(myProperty). Previous value was \(oldValue)") } }
You can implement one or both property observers in your property.
Getters and setters
Alternatively, you can use getters and setters for the saved property to solve your problem:
private var priv_property:Int = 0 var myProperty:Int{ get { return priv_property } set { priv_property = newValue } }
Computable properties do not actually retain the value. Instead, they provide a getter and an additional installer to retrieve and set other properties and values ββindirectly.
source share