I have a class with two properties whose value is set using didSet.
class MyClass {
var myProp1: Bool {
didSet {
self.myProp2 += blinks ? 1 : -1
}
}
var myProp2: Int {
didSet {
self.myProp1 = (midi % 2) != 0
}
}
}
This will result – as expected – in infinite recursion (myProp1 calling myProp2 calling myProp1...).
Is it possible to "turn off" the didSet behaviour when a property is called in another didSet? I know that didSet doesn't fire in init() and this should be similar.
I know about get/set and I know about the way to create a method, but I'm asking about suppressing didSet behaviour causing infinite recursion.
There's no way to disable a call to
didSet. In a case where you have two properties where setting one should update the other, you need a temporary property to be able to tell the otherdidSetnot to do anything when being called indirectly through the firstdidSet.Here's one working solution using a private
Booland some extra checks (I renamed the properties to match what you had in thedidSetblocks to make a working example):Here's some sample code that runs without any problems with infinite recursion:
Output: