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 trialDee Greene
8,508 PointsI cannot get my weather icons to display
I cannot get my weather icons to display. If I hardcode them to display as the default icon they will show. However, the default value is the only image that ever shows when the app loads. It never changes based on the enum that we created.
Dee Greene
8,508 PointsI set up the icon as an outlet in my ViewController....
@IBOutlet weak var currentWeatherIcon: UIImageView?
Then I add the code to update the icon but nothing...only default shows. this is in my view did load
func retrieveWeatherForecast() {
let forecastService = ForecastService(APIKey: forecastAPIKey)
forecastService.getForecast(coordinate.lat, long: coordinate.long) {
(let currently) in
if let currentWeather = currently {
// Update UI
dispatch_async(dispatch_get_main_queue()) {
// Execute closure
if let temperature = currentWeather.temperature {
self.currentTemperatureLabel?.text = "\(temperature)ΒΊ"
}
if let humidity = currentWeather.humidity {
self.currentHumidityLabel?.text = "\(humidity)%"
}
if let percipitation = currentWeather.precipProbability {
self.currentPrecipitationLabel?.text = "\(percipitation)%"
}
if let icon = currentWeather.icon {
self.currentWeatherIcon?.image = icon
}
if let summary = currentWeather.summary {
self.currentWeatherSummary?.text = "\(summary)"
}
self.toggleRefreshAnimation(false)
}
}
}
}
bellow is the code in my CurrentWeather.swift
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-couldy-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 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()
}
}
}
I know my button is correctly linked because if i change the default value it updates when the app is ran
2 Answers
Gabe Nadel
Treehouse Guest TeacherIf you place a breakpoint right after this line:
return UIImage(named: imageName)
Is the value for imageName what you expect, aka, not just the default?
Now do the same after this line:
icon = weatherIcon.toImage()
Is the value for icon what you expect, aka, not just the default?
Dee Greene
8,508 PointsI'm not sure what the error was but it works now. It's funny because when I ran the files from the downloads section it didn't show the icons either. I appreciate the help. The breakpoints helped me see what was going on but I guess it just fixed itself.
Gabe Nadel
Treehouse Guest TeacherJust a future bit of advice: when things in your UI aren't changing when you expect, often it's good to DELETE the app from the simulator or device and rebuild completely. Changes to .xibs and storyboards (as opposed to code) at times don't propagate when you simply rebuild. Not saying that is what happened here, but it's good to remember before it drives you nuts one day.
Dee Greene
8,508 PointsThank you so much!
Gabe Nadel
Treehouse Guest TeacherGabe Nadel
Treehouse Guest TeacherPlease post some code, as well as the names of the icons. Perhaps a screenshot of your project navigator window too. The community is happy to help, but we need more info.