[swift-users] Should Array's `append(_)` function cause `didSet`?

Marco S Hyman marc at snafu.org
Fri Jul 7 20:59:47 CDT 2017

>     init(keysAndValues:Dictionary<String, String>) {
>         self.keys.append(contentsOf: keysAndValues.keys)
>     }

> Above code doesn't call `didSet` in playground. My .swift file is similar and didn't call `didSet` either. However, if without a struct, `didSet` is called.

“If you don’t need to compute the property but still need to provide code that is run before and after setting a new value, use willSet and didSet. The code you provide is run any time the value changes outside of an initializer.”

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3.1).” iBooks. https://itun.es/us/jEUH0.l

Note “outside of an initializer”.  didSet and willSet are not called in initializers.

More information about the swift-users mailing list