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 Swift Enums and Structs Enums Enum Members and Raw Values

Peter Fuhrey
Peter Fuhrey
2,146 Points

Enum Challenge Task

I'm kinda confused why it's saying it was expecting to be type Int but it wasn't. Am I missing something here?

enum.swift
enum Speed: Int {
    case Slow = 10
    case Medium = 50
    case Fast = 100
}

var turtleSpeed = Speed(rawValue: 10) 

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Peter,

This challenge has caught a few users off guard because of the last example used in the previous video, what the task is actually asking for is the rawValue of Speed.Slow which we can get by simply using the below code.

var turtleSpeed = Speed.Slow.rawValue
Why didn't your code work?

When using the rawValue initializer we're simply creating a new instance of the enum for that given value, in this case you've specified you want the enum for the rawValue of 10 which is Speed.Slow, if the rawValue couldn't be found a nil value is returned instead.

Hope that helps.

Daniel Sattler
Daniel Sattler
4,867 Points

This is, how it should look like:

enum Speed: Int {
    case Slow = 10
    case Medium = 50
    case Fast = 100
}
var turtleSpeed = Speed.Slow.rawValue

Have a look on how you should call a enum member or itΒ΄s raw value ;-)

I hope this helped

Fernando Flores
Fernando Flores
4,209 Points

Remember that when you're creating an enum from a raw value it returns an optional. Something that you will learn in the next chapter In this challenge you need to retrieve the rawValue.

So instead of

            var turtleSpeed = Speed(rawValue: 10)

use this

            var turtleSpeed = Speed.Slow.rawValue

That should work!!

Good luck bro keep it going!!