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
From the course: Swift 5: Protocol-Oriented Programming
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
Weather app design2m 32s
-
Defining the UI using SwiftUI4m 20s
-
(Locked)
The WebServiceController protocol5m 13s
-
(Locked)
Working with a third-party API52s
-
(Locked)
Implementing the OpenWeatherMap controller4m 9s
-
(Locked)
Completing the OpenWeatherMap controller2m 42s
-
(Locked)
Introducing the view model3m 8s
-
(Locked)
Presenting weather data2m 39s
-
(Locked)
Challenge: Integrating a fallback service2m 34s
-
(Locked)
Solution: Integrating a fallback service4m 8s
-
-