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
classthat conforms toObservableObjectand 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 ourStateproperty to aStateObjectproperty:@StateObject var pet = ObservablePet()
- In
body, we want to account for our pet’s attributes, so add moreTextandTextFieldviews, pointing to their respective...