Understanding State and Binding
State management is something every developer has to deal with in application development. Imagine that you are developing a music player app. When a user taps the Play button, the button will change itself to a Stop button. In your implementation, there must be some ways to keep track of the application's state so that you know when to change the button's appearance.
In SwiftUI, it comes with a few built-in features for state management. In particular, it introduces a property wrapper named
@State. When you annotate a property with
@State, SwiftUI automatically stores it somewhere in your application. What's more, views that make use of that property automatically listen to the value change of the property. When the state changes, SwiftUI will recompute those views and update the application's appearance.
Doesn't it sound great? Or are you a bit confused with state management?
Anyway, you will get a better understanding of state and binding after going through the coding examples in this chapter. And, I've prepared a couple of exercises for you. Please do spare some time to work on it. This would help you master this important concept of SwiftUI.
To access the full content and the complete source code, please get your copy at https://www.appcoda.com/swiftui.