What is state in the swiftUI

Asked on 06/16/2025

1 search

In SwiftUI, "state" refers to a way of managing data that can change over time within a view. When you mark a view property as @State, SwiftUI manages the storage of that data and provides it back for the view to read and write. This allows the view to automatically update when the state changes, keeping the UI in sync with the underlying data.

For example, if you have a view that displays a rating, you can use state to keep track of the current rating value. When the user interacts with the UI to change the rating, SwiftUI automatically updates the view to reflect the new state. This is achieved without the need for manual updates or boilerplate code, as SwiftUI handles the updates behind the scenes.

Additionally, SwiftUI provides a concept called "binding," which creates a two-way reference to the state of another view. This allows different views to share and synchronize their state, ensuring that changes in one view are reflected in others.

For more detailed information, you can refer to the SwiftUI essentials session from WWDC 2024.