[swift-users] struct property changing calls didSet of the Struct each time, but class won't

Guillaume Lessard glessard at tffenterprises.com
Mon Nov 13 11:10:42 CST 2017


In your example, when `Foo` is a class, then `A` does not see a mutation since the storage is out-of-line. When `Foo` is a struct, it is stored in-line and a mutation does mutate `A`.

In other words, when what’s stored in `A` is a reference, the reference itself does not get mutated when you mutate your `Foo`. If you change the statement in your loop to `a.foo = Foo()`, you’ll see that the `didSet` gets invoked again.

Cheers,
Guillaume Lessard



More information about the swift-users mailing list