Michael Long
May 14, 2023

--

Close, but has a dangerous assumption. @ObserverdObject works correctly only when someone else holds a strong reference to the passed object.

If MyView has an ObservedObject view model, by the above definition the following would be correct.

var body: some View {

MyView(viewModel: MyViewModel())

}

And it's not. Every time the above view is refreshed a new view model will be created. See...

https://betterprogramming.pub/swiftui-the-unsafeobservedobject-quiz-467bb8554262

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

Responses (1)