iOS Build a Weather App Modeling Forecast Data Stubbing the Data

Cory Cromer
Cory Cromer
5,171 Points

How do I get rid of this error in the init method?

I reformatted the currentWeatherViewModel so it reformats the icon stored property from a string value to a UIImage instance. I did this through an extension using a func; however, when I use the func in the init method, I get an error reading, "'self' used before all stored properties are initialized." I do not know how to fix this. Any ideas?

import Foundation
import UIKit

//This ViewModel formats CurrentWeather for the ViewController through a custom init method
struct CurrentWeatherViewModel {
    let temperature: String
    let humidity: String
    let percipProbability: String
    let summary: String
    let icon: UIImage

    init(model: CurrentWeather) {
        let formattedTemperature = Int(model.temperature)
        self.temperature = "\(formattedTemperature)º"

        let formattedHumidity = Int(model.humidtiy * 100)
        self.humidity = "\(formattedHumidity)%"

        let formattedPercipProbability = Int(model.percipProbability * 100)
        self.percipProbability = "\(formattedPercipProbability)%"

        self.summary = model.summary

        let formattedIcon = convertIcon(named: model.icon)
        self.icon = formattedIcon

extension CurrentWeatherViewModel {
    func convertIcon(named: String) -> UIImage {
        switch named {
        case "clear-day": return #imageLiteral(resourceName: "clear-day")
        case "clear-night": return #imageLiteral(resourceName: "clear-night")
        case "rain": return #imageLiteral(resourceName: "rain")
        case "snow": return #imageLiteral(resourceName: "snow")
        case "sleet": return #imageLiteral(resourceName: "sleet")
        case "wind": return #imageLiteral(resourceName: "wind")
        case "fog": return #imageLiteral(resourceName: "fog")
        case "cloudy": return #imageLiteral(resourceName: "cloudy")
        case "partly-cloudy-day": return #imageLiteral(resourceName: "partly-cloudy")
        case "partly-cloudy-night": return #imageLiteral(resourceName: "cloudy-night")
        default: return #imageLiteral(resourceName: "default")

***error occurs under the convertIcon(named: model.icon) code