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

Errors with the custom Struct "Current" in the Building Simple Weather App lesson

Hi there, I believe I'm using the exact syntax as Pasan writes within the "Current.swift" file during his lesson on Building a Simple Weather App but I'm receiving two different errors that I'm unsure how to correct. Thanks in advance for any assistance.

Here are the two errors:

1: '()' is not convertible to 'UIImage'

2: Cannot convert the expression's type 'UIImage?' to type '()'

Error #1 occurs on the line of code that reads:

icon = weatherIconFromString(iconString)

And error #2 occurs on the last line of code in the file that reads:

return iconImage

And here's the complete syntax I'm using within this Current.swift file.

import Foundation
import UIKit

struct Current {
    var currentTime: String?
    var temperature: Int
    var humidity: Double
    var precipProbability: Double
    var summary: String
    var icon: UIImage?

    init(weatherDictionary: NSDictionary) {

        let currentWeather = weatherDictionary["currently"] as NSDictionary


        //currentTime = currentWeather["time"] as Int

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


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

        let iconString = 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 = .ShortStyle
        return dateFormatter.stringFromDate(weatherDate)
    }


    func weatherIconFromString(stringIcon: String) {
        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

    }

1 Answer

you need to add a return type of UIImage by placing -> UIImage at the end of the function header

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

    }

Thank you. That worked!