search by tags

for the user

adventures into the land of the command line

swift property observers

A much nicer writeup here, but here’s my summary.

Property observers observe and respond to changes in a property’s value. Property observers are called every time a property’s value is set, even if the new value is the same as the property’s current value.

var something: String! = "hellooooo" {
    didSet {
        myGroovyMethod()
    }
    willSet {
        myOtherGroovyMethod()
    }
}

something = "goodbye"

In the above code snippet, the order of operations are like this:

something is equal to "hellooooo"
willSet was called
myOtherGroovyMethod() was called
something is equal to "goodbye"
didSet was called
myGroovyMethod() was called