0

I can't get my Icons to display, I get an error on line: return UIImage(named: imageName)

The error says: Missing return in a function expected to return "UIImage?"

If anybody could help me get rid of this annoying error, Thank you in advance.

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"


        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)
            }
        }
    }
Katz
  • 826
  • 3
  • 19
  • 40

1 Answers1

1

Your return statement is in the .PartlyCloudyNight case of your switch. You need to move it outside the switch.

AdamPro13
  • 7,232
  • 2
  • 30
  • 28
  • You may also be interested in making the string values for each enum the same as their respective file name (without the .png, which isn't necessary). Then you could simplify your function to `return UIImage(named: self.rawValue())` – AdamPro13 Aug 18 '15 at 23:50