Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Martin Lynnerup
Martin Lynnerup
3,134 Points

How do I switch on an Enum in Swift, depending on the input I provide?

I have created the below switch statement to return a Double value depending on what string I input in weightType.

I would like to it with an Enum instead, so that it's more flexible. I though it was an easy change but I have tried all kinds of things and I can't make it work.

Can someone please help me? Thx!

var weightType: String = "kettlebell"

func returnWeightLimit() -> Double {
    switch weightType {
    case "barbell": return(132.0)
    case "kettlebell": return(70.5)
    case "dumbbell": return(115.0)
    default: return(0.0)
    }
}

let weightLimit = returnWeightLimit()

3 Answers

Marlon Burnett
PLUS
Marlon Burnett
Courses Plus Student 12,917 Points

Hey Martin, in that case you could do something like that, i guess...

enum WeightType: String {
    case barbell
    case kettlebell
    case dumbbell
}

extension WeightType {
    var weightLimit: Double {
        get {
            switch(self){
                case .barbell: return 132.0
                case .kettlebell: return 70.5
                case .dumbbell: return 115.0
            }
        }
    }
}

let weightType = WeightType(rawValue: "kettlebell")
if let weightType = weightType {
    let weightLimit = weightType.weightLimit
    print("Weight limit: \(weightLimit)")
}
Marlon Burnett
PLUS
Marlon Burnett
Courses Plus Student 12,917 Points

Hi Martin!

I think that would be your best option

enum WeightType {
    case barbell
    case kettlebell
    case dumbbell
}

extension WeightType {
    var weightLimit: Double {
        get {
            switch(self){
                case .barbell: return 132.0
                case .kettlebell: return 70.5
                case .dumbbell: return 115.0
            }
        }
    }
}

let weightType: WeightType = .kettlebell
let weightLimit = weightType.weightLimit

Feel free to reach out for me if you need further help! Best, Marlon

Martin Lynnerup
Martin Lynnerup
3,134 Points

Hi Marion,

Thanks a lot for the quick answer!

I should probably have mentioned that I still want an input value of type String. That's where it gets tricky I think...

Jeremy Conley
Jeremy Conley
16,657 Points
enum WeightType {
    case Barbell
    case KettleBell
    case Dumbbell
}

var weightType: WeightType = .KettleBell

func returnWeightLimit() -> Double {
    switch weightType {
    case .Barbell: return(132.0)
    case .KettleBell: return(70.5)
    case .Dumbbell: return(115.0)
    default: return(0.0)
    }
}

let weightLimit = returnWeightLimit()