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 Displaying Icons

Trick Spades
Trick Spades
3,182 Points

Undeclared type 'CurrentWeather' in Stormy App

For some reason it says I have an undeclared type called Current Weather, when I have the struct and class "CurrentWeather"

I didn't have this error until I tried to run Stormy, then it showed me the error.

The error occurs in ForecastService:

func getForecast (lat: Double, long: Double, completion: (CurrentWeather? -> Void)) {
    if let forecastURL = NSURL(string: "\(lat),\(long)", relativeToURL: forecastBaseURL) {

        let networkOperation = NetworkOperation(url: forecastURL)

        networkOperation.downloadJSONFromURL {
            (let JSONDictionary) in
            let currentWeather = self.currentWeatherFromJSON(JSONDictionary)
            completion(currentWeather)
        }
    } else {
        print("Could not construct valid URL")
    }

}

func currentWeatherFromJSON(jsonDictionary: [String: AnyObject]?) -> CurrentWeather? {
    if let currentWeatherDictionary = jsonDictionary?["currently"] as? [String:AnyObject] {
        return CurrentWeather(weatherDictionary: currentWeatherDictionary)
    } else {
        print("JSON dictionary returned nil for 'currently'")

Here is my CurrentWeather struct:

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,
        let weatherIcon: Icon = Icon(rawValue: iconString) {
        icon = weatherIcon.toImage()
    }
}

}

4 Answers

Trick Spades
Trick Spades
3,182 Points

I figured it out. In the course, Pasan puts the Icon enum before the CurrentWeather struct, which for some reason in XCode 7, makes CurrentWeather unaccessible. I switched the CurrentWeather struct with the Icon enum and it works now. :)

all i can think to do is change "CurrentWeather" to "currentWeather"... but again i am learning like you. i have also just finished that course.

Trick Spades
Trick Spades
3,182 Points

I don't think that's it. Because currentWeather is a constant and CurrentWeather is a class. I didn't have any errors until I tried to run, and now for some reason my Forecast class doesn't have access to the CurrentWeather struct

what is your error telling you? often if you just google the error you can find quick explanations by professionals. i tend to do it frequently when building my own apps, its a bad practice but try typing your error directly into a search engine

Trick Spades
Trick Spades
3,182 Points

it says undeclared type 'CurrentWeather', but it is declared in a different class, so I don't see why it wouldn't work. I guess I'll have to do some more research before I can continue in the course

Tukang Keboen
Tukang Keboen
2,840 Points

I have recheck and followed Mr Pasan Instructions at all.... instead my icon still not change either the information humidity and rain

dunno where going wrong

Current Weather

<p> 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" 

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

    switch self {
    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"
    }

return UIImage(named: imageName)
}

struct CurrentWeather {

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




init(weatherDictionary: [String: AnyObject]) {
    tempature = 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,
        let weatherIcon: Icon = Icon(rawValue: iconString){
            icon = weatherIcon.toImage()
    }

}

</p>