Understanding Observable Objects
Now that we understand reactive flows, we can dive a little deeper into more complex situations. In this recipe, we will look at Observable Objects and see how they can help make our apps more reactive to data structures.
How to do it...
Let’s start where we left off in our previous recipe:
- First, create a new
class
that conforms toObservableObject
and will hold all of our pet’s information:class ObservablePet : ObservableObject {
@Published var name = «»
@Published var age = «»
@Published var breed = «»
init() { }
}
- Next, in
ContentView
, change ourState
property to aStateObject
property:@StateObject var pet = ObservablePet()
- In
body
, we want to account for our pet’s attributes, so add moreText
andTextField
views, pointing to their respective...