Understanding the state
In SwiftUI, a state is a piece of data that can change. When the state changes, the view that depends on the state is automatically refreshed.
You can declare a state in a SwiftUI view by using the @State
property wrapper. For example, see the following:
struct ContentView: View { @State private var name: String = "Bella" }
Here, name
is a state that is stored as a string. You can then use this state to display dynamic content in your view:
struct ContentView: View { @State private var name: String = "Bella" var body: some View { Text("Hello, \(name)") } }
To change the state, we can assign a new value to the @State
property. For example, see the following:
struct ContentView: View { @State private var name: String = "Bella" ...