In the book (version 1.0.3) the author writes on page 280 of the PDF:
@ObservedObject var model: ReaderViewModel
You don’t inject the model when initializing the view anymore. Instead, you bind the model so that, any time its state changes, your view will receive the latest data and generate its new UI “snapshot”.
The @ObservedObject wrapper does the following:
- Removes the property storage from the view and uses a binding to the original
model instead. In other words, it doesn’t duplicate the data.
- Marks the property as external storage. In other words, it denotes that the piece
of data is not owned by the view.
However, in class SceneDelegate, func scene( …)
isn’t the chapter 15 “final” code injecting the model when initializing the view, with this, below?
let viewModel = ReaderViewModel()
let rootView = ReaderView(model: viewModel)
If we only want to use “a binding to the original model”, shouldn’t viewModel be a data member of some class and then we pass a reference to that data member to ReaderView?
If the chapter 15 “final” code is correct, what is retaining the reference to the viewModel? Because @ObservedObject “removes the property storage from the view” ?