Why Does SwiftUI Use Structs Instead of Classes?
I mean, Flutter uses classes, so why doesn’t SwiftUI?
If SwiftUI were a person, it’d be that chill friend who loves simplicity, hates drama, and can’t stand overcomplicating things. And when it came to choosing between structs and classes for its views, SwiftUI made it clear: it was time to leave classes on “read” and fully commit to the low-maintenance bliss of structs.
But why would a fancy framework like SwiftUI — a UI toolkit that could have had anything — choose humble, no-frills structs over their beefier, more traditional class counterparts? Especially when the UI hotshot, Flutter, swears by classes? It’s a little like choosing the basic, dependable sedan over the tricked-out sports car. But trust me, there’s method to this madness (I’m exaggerating stuff).
1. SwiftUI Loves the “One-and-Done” Attitude of Structs
Think of SwiftUI like someone who doesn’t have time for complicated relationships. Structs
are like single-use, disposable versions of your views. Every time the view changes, SwiftUI just grabs a fresh copy of your struct
and rolls with it. Easy peasy. With classes
, you’d have this messy, shared state all over the place, and suddenly your UI’s got baggage. Ain’t nobody got time for that.