I read through this SO didSet q&a and Apple’s Property Observers and a few other posts. What I can’t seem to wrap my head around is what is the benefit of using didSet when mutating a variable when if you change the variable without using a property observer it’s going to change anyway?
Scenario 1:
var someVal = 0
someVal = 10
// someVal now holds 10
Scenario 2:
var someVal: Int = 0{
didSet{
}
}
someVal = 10
// again someVal now holds 10
Scenario 3:
var someVal: Int = 0{
didSet{
if someVal > oldValue{
someVal = newValue
}
}
}
someVal = 10
// once again someVal holds a value of 10
The only thing I see in Scenario 3 is that if the condition isn’t met then someVal won’t change. But instead of adding it inside the didSet I can simply do this and the same exact thing will occur.
var someVal = 0
var anotherVal = 10
if someVal < anotherValue{
someVal = anotherValue
}
// once again someVal holds a value of 10
So other then adding a condition inside a didSet observer what is the benefit?