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

javier graterol
javier graterol
6,056 Points

enum challenge Create a variable called turtleSpeed and assign it the Raw value of the member Slow.

works on Xcode. But here it says

Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors.

enum Speed: Int { case Slow = 10, Medium = 50, Fast=100 } if let turtleSpeed = Speed(rawValue: 10) { Speed.Slow.rawValue }

2 Answers

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

javier graterol

The answer to the question is simply:

var turtleSpeed = Speed.Slow.rawValue

This is assuming you've modified the enum to have raw values. In your code, you are creating an instance of the enum by using the rawValue method to match the integer value passed in. The enum already has a default raw value for Slow, so you don't need to create a new instance and check the optional

Chris Bernardi
Chris Bernardi
3,994 Points

@javier I'm not sure why you are seeing any errors. I copied and pasted your code above into a new playground and got 50. Is what you have there, the only code in the file?

javier graterol
javier graterol
6,056 Points

Yes, that is the only file. The task is "Create a variable called turtleSpeed and assign it the Raw value of the member Slow."

Chris Bernardi
Chris Bernardi
3,994 Points

Although, I can't get a correctly complied code, I do see to possible errors. 1) it calls for a variable thus var not let. 2) It wants the value for Slow not Medium

javier graterol
javier graterol
6,056 Points

Nope, if you change those values the "error" keeps happening.

Chris Bernardi
Chris Bernardi
3,994 Points

I understand that doesn't solve it but its still incorrect to use them. If you don't start there, no fix will work.