반응형
Property observer in swift
According to apple’s docs
Property observers observe and respond to changes in a property’s value. Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value .
Property that has observers must declared as var. Because It must be mutable to track changes.
Also you need to set an initial value of property.
This value could be optional or any other type.
One thing to keep in mind is WillSet and DidSet is not called on setting initial value in init(). After initial value set, it will get called every time even if you assign the same value to it.
WillSet and DidSet both have a default parameters newValue and oldValue.
You can see it in examples.
You can use WillSet and DidSet on sending notifications, synchronization, making ui change accordingly etc..
Happy coding :)
반응형
그리드형
'개발 > swift' 카테고리의 다른 글
Swift UILabel linke break mode (0) | 2020.10.22 |
---|---|
Swift divide result 0 issue (0) | 2020.10.20 |
Swift wkwebview message handler (0) | 2020.10.17 |
Swift codable protocol (0) | 2020.10.17 |
Swift How to get range of substring? (0) | 2020.10.17 |