Sitemap
The Swift Cooperative

Authors publishing advanced articles on Swift and SwiftUI application development.

Member-only story

SwiftUI: Why You Need AnyView

16 min readSep 5, 2023

--

Press enter or click to view image in full size
Photo by Christian Ladewig on Unsplash

AnyView has problems. Really. Ask any seasoned iOS developer and they’ll tell you that AnyView has problems.

Problems so significant, in fact, that the majority of SwiftUI developers completely ignore it and some shops prohibit its use altogether.

It’s that bad.

Ask why it’s bad, however, and most developers will begin to avert their eyes, stare off into the distance, and start mumbling something about how it “destroys” view hierarchies and kills performance.

Press the question even further and they’ll start to hem and haw, and then, finally, admit that they don’t know why it’s bad or how it kills performance. Just that they’ve been told that it’s bad.

And didn’t Apple say something about avoiding it in one of their WWDC videos?

They did. But more on that later.

But the truth is that AnyView isn’t bad. And that there are valid reasons for its existence. True, there are a few special cases where using it can cause performance problems. But you know what?

You can trigger those same exact issues in “normal” SwiftUI code without using AnyView.

--

--

A Lead Software Engineer who writes about Apple, Swift, SwiftUI, and technology. Also author of the open source projects Factory and Navigator.

The Swift Cooperative
The Swift Cooperative

Published in The Swift Cooperative

Authors publishing advanced articles on Swift and SwiftUI application development.