Michael Long
Oct 19, 2023

--

DO NOT DO THIS.

Every so often this pops up and it's a time bomb just waiting to cause unintended side effects.

Why? Because SwiftUI considers the view inside of the if to be different from the view inside of the else. It's positional identity. As such, having your condition change can cause every descendent view in the view hierarchy to lose state when the old view hierarchy is destroyed and the new view hierarchy is created.

For an in-depth explanation see the following article (not mine) on objc.io.

https://www.objc.io/blog/2021/08/24/conditional-view-modifiers/

--

--

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