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

Setting Icon Images- Build a simple weather app in Swift

Help!

I followed Pasan's code to the letter and the app does not work I'm getting an error message : NSString is not a subtype of UIImage on the "let iconString = icon = currentWeather["icon"] as String" line. Can anyone please help? This build isn't working at all.

import Foundation
import UIKit

struct Current {

    var currentTime: String?
    var temperature: Int
    var humudity: Double
    var preciProbability: Double
    var summary: String
    var icon: UIImage?


    init(weatherDictionary: NSDictionary) {
        let currentWeather = weatherDictionary["currently"] as
           NSDictionary



        temperature = currentWeather["temperature"] as Int
        humudity = currentWeather["humidity"] as Double
        preciProbability = currentWeather["precipProbability"] as Double
        summary = currentWeather["summary"] as String


        let currentTimeIntValue = currentWeather["time"] as Int
         currentTime = dateStringFromUnixTime(currentTimeIntValue)

         let iconString = icon = currentWeather["icon"] as String
          icon = weatherIconFromString(iconString)
    }

    func dateStringFromUnixTime(unixTime: Int) -> String  {
        let timeInSeconds = NSTimeInterval(unixTime)
        let weatherDate = NSDate(timeIntervalSince1970:timeInSeconds)
        let dateFormatter = NSDateFormatter()
        dateFormatter.timeStyle = .MediumStyle 
        dateFormatter.timeStyle = .ShortStyle

        return dateFormatter.stringFromDate(weatherDate)

    }

    func weatherIconFromString(stringIcon: String) -> UIImage {
        var imageName: String

        switch stringIcon {
        case "clear-day":
            imageName = "clear-day"
        case "clear-night":
            imageName = "clear-night"
        case "rain":
            imageName = "rain"
        case "snow":
            imageName = "snow"
        case "sleet":
            imageName = "sleet"
        case "wind":
            imageName = "wind"
        case "fog":
            imageName = "fog"
        case "cloudy":
            imageName = "cloudy"
        case "partly-cloudy-day":
            imageName = "partly-cloudy"
        case "partly-cloudy-night":
            imageName = "cloudy-night"
        default:
            imageName = "default"
        }

        var iconImage = UIImage(named: imageName)
        return iconImage!


}
}

3 Answers

let iconString = icon = currentWeather["icon"] as String <--- is improper syntax.

let iconString = currentWeather["icon"] as String <---correct answer

Thank you Marion. There have been several typos in the videos and this is just one of three, so thank you for pointing me in the right direction.

And speaking of typos my name is Marlon not Marion LOL. This was comedy gold I couldn't pass it up lol....you're welcome sir.

I'm sorry Marlon for getting your name wrong. My reading glasses were not at hand when I answered you and this pale grey-on-white color scheme Treehouse has isn't so kind to fifty-year-old eyes!

Lol...it's okay, I say my name to clients and then they still call me whatever they want anyway, so by far in my book sir you got them beat. I just thought it was funny since we was talking about typos, I'm a jokester, so I couldn't pass up a chance at a good joke lol. Yeah I wish Treehouse let us have some fancy colors and fonts and such that would be nice.