Question about valid uses of `ObservableObject`'s synthesized `objectWillChange`

The synthesized objectWillChange creates an ObservableObjectPublisher once and installs it into all the @Published properties using runtime introspection. When you access objectWillChange, it will take out the installed instance and return it to you.

Since there's no @Published properties in your class, there's no storage objectWillChange could install the publisher into, so it just creates and returns a new instance every time you access objectWillChange.

So in your case you should either make your name property @Published, or you provide a custom implementation of the objectWillChange property (trivially, by adding a stored constant to your ViewModel class)

If you're curious how it's implemented, you can refer to my attempt at replicating this behavior.

4 Likes