Understanding Variables in SwiftUI
What are variables and how are they used in SwiftUI while prototyping
I spent some time experimenting with variables in SwiftUI. In this blog post, we'll explore what variables are and how to use them effectively in SwiftUI.
So what are variables?
As a designer, you might have heard these terms while working in Figma, and as an engineer, I know you often use them all the time. Think of variables as buckets that store varying values. In the above video, you can see the shape changes based on the slider movement because the color and radius are variables connected to the sliders that change values based on user interactions. Variables can also help pass information between views.
Three types of variables in SwiftUI:
@State
@Binding
@ObservedObject
State variable: It stores data that can change, such as the toggle state
Binding variable: Connects the parent and child view
ObservedObject Variable: Tracks changes
Variables help us create dynamic interfaces
While working with SwiftUI, you will be declaring the code in this manner. Here you can see we use State variables to have varying height, size, color, etc.
Then we use binding variables ($cornerRadius) for the slider to change the corner radius of the square.
So that’s about variables this week.
Thank you for reading!
If you create something, feel free to share it with me! Also, don’t forget to subscribe to stay updated on all these experiments and demos.
🧑💻👩💻 If you're interested, get started and learn SwiftUI!
👉 Check out the Prototyping with SwiftUI course By John Rodrigues from here