This is a very elementary question but somehow I am not getting it. I have a button in a view which when tapped increases the value of an integer in it by one. I want to know if the value of the variable is changed or not. I know the same could be easily achieved by using Boolean value instead of a Integer but I my specific case use of integer is necessary. I just want to know if the value of a variable is changed or not.
Asked
Active
Viewed 814 times
-1
-
1Variables have something called [property observers](https://docs.swift.org/swift-book/LanguageGuide/Properties.html). – Rakesha Shastri Dec 26 '19 at 08:28
1 Answers
1
Do you mean something like this?
var counter = 0 {
didSet {
if oldValue != counter {
print("counter was changed from \(oldValue) to \(counter)")
}
}
}
Output would look like this:
counter was changed from 0 to 1
counter was changed from 1 to 2
counter was changed from 2 to 3

2h4u
- 504
- 3
- 8