Does SwiftUI View Not Compile? Time To Start Making It Lighter
It’s necessary to be resourceful in unexpected situations when programming.
Well, this post is about a specific issue that I recently met in SwiftUI and I would like to share here. Comparing to UIKit, SwiftUI is still an infant, yet it’s not that new so we still have many dark points about it. Regardless, only by using it under various scenarios one can really see its ins and outs, and discover weaknesses that add friction to the development process.
Such a friction came to slow down my work significantly into an app just a few days ago, where a SwiftUI view could not be compiled, and as Xcode said, “in reasonable time”. Issues like that turn me towards the opinion of those people saying that most probably SwiftUI is not production-ready yet; a statement that I mostly disagree in general. Admittedly, chances are that SwiftUI won’t do everything we want out of the box, but with a few tweaks based mostly on UIKit (or AppKit on macOS) we can make everything work just fine. Despite any problems, I personally vote for SwiftUI and I’ll keep using it, having UIKit and AppKit as my assistants to my ongoing and future programming tasks (bold statement, but I don’t see myself going back to using pure UIKit or AppKit again).
Anyway, my intention is not to make a lecture about SwiftUI here, but to be practical and focus on a real issue and how I worked around it, putting things back on track again. Before that, I’d like to make clear that it’s possible the problem I’ll describe next to be known, or not to be a problem at all, just a natural behavior. Or it could be related to a deep detail about how SwiftUI operates internally. In any case, something that I’m just not aware of. But even in that case, the proposing solution presented later could still be interesting to use.
That said, it’s time to start being specific. To make it easy to follow along, I’ll be providing sample code while describing, so you can try things by yourself too.
The original issue
To give the general context, the “problematic” view for me was a custom container; a view that was presenting a really small number of child views conditionally. However, that container view…