swiftclosuresstrong-referencesdidset

Are willset and didset considered closures in Swift?


I understand the purpose of willset and didset my I am not sure if they are considered closures.

If they were closures, shouldn't the following code produce a strong reference cycle?

var myProperty : Int = 0 {
    didSet { self.callMyMethod() }
}

Solution

  • No, they are not closures. You can think of it like a special type of function which is not directly accessible; it will only be called when the property changes. (The function is named myapp.MyStruct.myProperty.didset; you can see this in the debugger.)