From the course: Swift 5: Protocol-Oriented Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Introducing the view model

Introducing the view model

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

Introducing the view model

- [Instructor] We can now fetch current weather data using the OpenWeatherMap service and we've also got the user interface. So, let's connect the dots. The missing piece is the view model. It acts as a padding between the model and the view without referencing the view. We'll introduce a new class that represents the view model. First, add a new group and call it ViewModel. Then create a new swift file called WeatherViewModel. Now let's declare the ViewModel class. WeatherViewModel adopts the observable object protocol to enable data binding. Next, I declare a property called the weatherInfo and initialize it to an empty string. A market with the published property wrapper. This allows Swift to observe this property and update the view whenever this property changes. The view model exposes a method to fetch the weather information. It relies on the model that is the open weather map controller to provide this…

Contents