@State variables in SwiftUI

Vik Denic
vik’s code journal
1 min readJun 12, 2019

Apple joined the declarative UI movement in full-force with its announcement of SwiftUI at WWDC 2019. The new framework appears strikingly similar to Google’s Flutter, a portable toolkit that has already gained considerable momentum and adoption from the developer community in its relatively young existence.

And for iOS devs, SwiftUI comes with a ton of exciting new documentation and WWDC tutorials. As both a Flutter and iOS dev (depending on the project, of course), I was most excited to see how SwiftUI possibly differentiated itself from Flutter.

Upon watching the first WWDC SwiftUI session, a few key differences were quickly apparent. While both frameworks provide a hot-reload to reflect your code changes in record-time, SwiftUI’s canvas features (although currently only available with MacOS Catalina) do seem advantageous, namely the ability to drag-and-drop view elements / modifiers.

And my favorite feature of SwiftUI is the addition of @State variables. A common challenge in declarative programming is updating an app’s UI dependent on the state of its data. In Flutter, this means commonly calling setState().

But with SwiftUI, the UI is automatically re-rendered when a @State variable is changed 🙌

Simple, straightforward and.. Swift!

--

--