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

Bradley Maskell
Bradley Maskell
8,858 Points

Swift Enums printSpeed Challenge

import UIKit

enum Speed {
    case Slow(String)
    case Medium(String)
    case Fast(String)
}


func printSpeed (speed: Speed) -> String  {
    switch speed {
    case .Slow(let slow):
        println("Slow")
    case .Medium(let medium):
        println("Medium")
    case .Fast(let Fast):
        println("Fast")
    default:
        return "Unknown"
    }
}

I've had no luck. Please help.

5 Answers

Here is the answer! I had to do what Jennifer Jones said and use raw values instead of associated values. Thanks again, Jennifer.

enum Speed: String {
    case Slow = "Slow"
    case Medium = "Medium"
    case Fast = "Fast"
}

func printSpeed (speed: Speed) -> String{
    switch speed {
        case Speed.Slow:
            return Speed.Slow.rawValue
        case Speed.Medium:
            return Speed.Medium.rawValue
        case Speed.Fast:
            return Speed.Fast.rawValue
        default:
            return "Unknown"
    }
}

You are very welcome!

This challenge got me as well lol. Here's a hint which will allow you to get it straight away:

Use Raw Values instead of Associated Values

This was a huge help and I got the answer right. I'll post it now.

Bradley Maskell
Bradley Maskell
8,858 Points

Hi Jennifer Jones,

It's still not working for me.

enum Speed {
    case Slow(String)
    case Medium(String)
    case Fast(String) }

func printSpeed (speed: Speed) -> String {
    switch speed {
        case .Slow(let slow):
            return slow.rawValue
        case .Medium(let medium):
            return medium.rawValue
        case .Fast(let fast):
            return fast.rawValue
        default:
            return "Unknown" }
}

Looks like you're not formatting the markdown properly.

Next time, use the name of the programming language "swift" after the three backticks.

David Galindo
PLUS
David Galindo
Courses Plus Student 2,294 Points

Ohh!!! This makes more sense now. I was having a hard time with this challenge.

Sure thing!

No problem. Glad I could help