5

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() }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
agy
  • 2,804
  • 2
  • 15
  • 22

1 Answers1

5

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.)

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 1
    But if functions are special cases of closures, and willSet and didSet are special cases of functions, then surely they are also special cases of closures? From _The Swift Programming Language_: "Global and nested functions, as introduced in Functions, are actually special cases of closures." – 雨が好きな人 Oct 15 '16 at 14:49