Swift - didSet{...} Property Observer
if you're not familiar with the 'didSet' property observer - this is for you:
with this simple trick you can execute a code when a property is changed.
e.g. - if we want a counter to count every time a property changed (in this example - when the button is pressed):
This is how it works: just add { didSet { //your code }} like this:
struct Entity{
??? private(set) var changedCounter = 0
??? var string: String? {
??????? didSet{
??????????? changedCounter += 1
??????? }
??? }
?? ?
??? init(str: String){
??????? self.string = str
??? }
}
and here's the code for the ViewController to test it:
we create an entity from above, and each button press will change the string, and consequently the counter updates.
screenshot for the entity :
CEO and security engineer
4 个月???? ??? ?? ?? ??????! ??? ????? ???? ?????? ???: ?????? ????? ??? ??????? ?????? ??????, ?????? ?????? ??????,?????? ????? ????????. https://chat.whatsapp.com/IyTWnwphyc8AZAcawRTUhR