Here's what I created to understand didSet
.
--Can be recalled when the value of a property changes.
--didSet
can be reset according to the changed value.
class Test {
var num: Int = 0 {
willSet {
print("The value of num is\(newValue)Is likely to be changed to")
}
didSet {
print("The value of num is\(self.num)Changed to")
if self.num > 100 {
print("Since num was 100 or more, return it to 20")
self.num = 20
}
}
}
}
var test = Test()
test.num = 102
print(test.num)//20
Recommended Posts