SwiftUI Tutorial: State and Binding
Understanding how State and Binding works
As a general idea, state and binding are property wrappers that deals with reading and writing a value. It is an important core concept for creating an app in SwiftUI.
This tutorial is part of my SwiftUI Tutorial series.
What is State?
In Swift language, modification of values inside a struct is not allowed (immutable) because it is considered as type of value. Example, I have this declared variable inside a struct:
var isPlaying: Bool = false
The isPlaying being false cannot be modified to true when the program is running, as long as it is under a struct. The only way for it to be modified under a struct, is by moving the storage into a shared memory. This can be done by wrapping it with state property like this:
@State var isPlaying: Bool = false
Setup a New Project
To understand more about state, create a new project or open an exisiting one that you use for practice. You can refer to this link if you need a step-by-step process on how to do it.
Let’s create a player button that changes appearance every time the user taps.