Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift, does resetting the property inside didSet trigger another didSet?

Tags:

I'm testing this and it appears that if you change the value within didSet, you do not get another call to didSet.

var x: Int = 0 {
    didSet {
        if x == 9 { x = 10 }
    }
}

Can I rely on this? Is it documented somewhere? I don't see it in the Swift Programming Language document.

like image 356
Rob N Avatar asked Oct 02 '16 16:10

Rob N


2 Answers

I also thought, that this is not possible (maybe it wasn't in Swift 2), but I tested it and found an example where Apple uses this. (At "Querying and Setting Type Properties")

struct AudioChannel {
    static let thresholdLevel = 10
    static var maxInputLevelForAllChannels = 0
    var currentLevel: Int = 0 {
        didSet {
            if currentLevel > AudioChannel.thresholdLevel {
                // cap the new audio level to the threshold level
                currentLevel = AudioChannel.thresholdLevel
            }
            if currentLevel > AudioChannel.maxInputLevelForAllChannels {
                // store this as the new overall maximum input level
                AudioChannel.maxInputLevelForAllChannels = currentLevel
            }
        }
    }
}

And below this piece of code, there is the following note:

In the first of these two checks, the didSet observer sets currentLevel to a different value. This does not, however, cause the observer to be called again.

like image 197
FelixSFD Avatar answered Sep 29 '22 18:09

FelixSFD


From Apple docs (emphasis mine):

Similarly, if you implement a didSet observer, it’s passed a constant parameter containing the old property value. You can name the parameter or use the default parameter name of oldValue. 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.

So, assigning a value in didSet is officially OK and won't trigger an infinite recursion.

like image 31
FreeNickname Avatar answered Sep 29 '22 17:09

FreeNickname