Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift Adding Weather Icons Streamlining Our Icon Enum

Errors in struct CurrentWeather {}

I typed in the code along with this video and got three errors, so I copied the code from the teacher's notes and get the same errors. Here they are:

line 79: "Expected '{' after 'if condition" line 80: "Use of unresolved identifier 'iconString' line 81: "Variable used within its own initial value"

I'd really like to complete this course and now I'm stuck. Please help if you can.

Thanks!

1 Answer

Raed Alahmari
Raed Alahmari
12,966 Points

Use this ^_^

import UIKit
import Foundation


enum Icon: String {
    case ClearDay = "clear-day"
    case ClearNight = "clear-night"
    case Rain = "rain"
    case Snow = "snow"
    case Sleet = "sleet"
    case Wind = "wind"
    case Fog = "fog"
    case Cloudy = "cloudy"
    case PartlyCloudyDay = "partly-cloudy-day"
    case PartlyCloudyNight = "partly-cloudy-night"
}


struct CurrentWeather {

    let temperature: Int?
    let humidity: Int?
    let precipProbability: Int?
    let summary: String?
    var icon: UIImage? = UIImage(named: "default.png")

    init(weatherDictionary: [String: AnyObject]) {
        if let temperatureInC = weatherDictionary["temperature"] as? Int {
            temperature = Int(temperatureInC - 32) * 5 / 9
        } else {
            temperature = nil
        }
        if let humidityFloat = weatherDictionary["humidity"] as? Double {
         humidity = Int(humidityFloat * 100)
        } else {
            humidity = nil
        }
        if let precipFloat = weatherDictionary["precipProbability"] as? Double {
         precipProbability = Int(precipFloat * 100)
        } else {
            precipProbability = nil
        }
        summary = weatherDictionary["summary"] as? String

        if let iconString = weatherDictionary["icon"] as? String {
            icon = weatherImageFromIconString(iconString)
        }
    }

    func weatherImageFromIconString(iconString: String) -> UIImage? {
        var imageName: String

        if let iconValue = Icon(rawValue: iconString) {
            switch iconValue {
            case .ClearDay:
                imageName = "clear-day.png"
            case .ClearNight:
                imageName = "clear-night.png"
            case .Rain:
                imageName = "rain.png"
            case .Snow:
                imageName = "snow.png"
            case .Sleet:
                imageName = "sleet.png"
            case .Wind:
                imageName = "wind.png"
            case .Fog:
                imageName = "fog.png"
            case .Cloudy:
                imageName = "cloudy.png"
            case .PartlyCloudyDay:
                imageName = "cloudy-day.png"
            case .PartlyCloudyNight:
                imageName = "cloudy-night.png"
            }

        } else {
            imageName = "default.png"
        }

        return UIImage(named: imageName)
    }

}