Michael Long
Dec 1, 2023

--

Injecting the VM isn't an optimal solution, as it exposed the view's internals to the outside world. That complicates each and every call site that might want to display the view, while at the same time making it much, much harder to refactor the view should the need arise.

Further, your last example is problematic. Your passed VM is what's used as that overrides the assigned, thunked wrappedValue. But that thunk is only called once for the lifetime of the view, which means that it can't be "reset" to to "new toggle".

That's unexpected behavior.

--

--

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.

No responses yet