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 Assigning An Image

Matt Frerichs
Matt Frerichs
4,015 Points

Use of local variable warning 'weatherImageFromIconString'

In the video he gets a warning on the same line that I am, but instead of getting the "Cannot assign to 'icon' in 'self'" warning, I am getting "Use of local variable 'weatherImageFromIconString' before its declaration". Any idea why I am getting this?

Here is my code:

//start
import Foundation
import UIKit


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]) {
        temperature = weatherDictionary["temperature"] as? Int
        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)

        }

    }
}

//end

1 Answer

Hello,

The problem is that you have declared your weatherImageFromIconString method inside the initializer method. Move it out and you should be good to go!

Cheers!