Why recursion does not occur, when I assign a value to a property within its own didSet observe? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why recursion does not occur, when I assign a value to a property within its own didSet observe?

Here is the code: class StepCounter { var totalSteps: Int = 0 { willSet(newSteps) { print("About to set totalSteps to \(newSteps)") } didSet { totalSteps+=10// I thought recursion should occur (willSet and didSet should be called every time) print ("\(totalSteps - oldValue) steps") } } } let stepCounter = StepCounter() stepCounter.totalSteps = 50

16th Dec 2015, 12:46 PM
Lilit d'Khachatryan
Lilit d'Khachatryan - avatar
1 Answer
+ 1
I guess that's how Swift was designed. The official Apple documentation says: "If you assign a value to a property within its own didSet observer, the new value that you assign replaces the one that was just set."
26th May 2016, 1:08 PM
James Flanders