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

Johannes Böhm
Johannes Böhm
5,957 Points

Cant assign the rawValue of 10 to turtleSpeed

I tried following code to assign the rawValue of 10 to turtleSpeed, but I can not find out why this is not the right solution. Any hints?

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

var turtleSpeed = Speed(rawValue: 10)

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

var turtleSpeed = Speed(rawValue: 10)

2 Answers

Stone Preston
Stone Preston
42,016 Points

task 1 states: Create a variable called turtleSpeed and assign it the Raw value of the member Slow.

to access the raw value of an enum member you use the raw value property:

EnumName.memberName.rawValue

so to create a variable called turtleSpeed and assign it the raw value of slow you use:

var turtleSpeed = Speed.Slow.rawValue
Henry Moran
Henry Moran
10,516 Points

Here's how I approached it:

Q: Create a variable called turtleSpeed and assign it the Raw value of the member Slow.

Step 1: Create a variable called turtleSpeed

var turtleSpeed

Step 2: Assign the raw value of the member Slow

var turtleSpeed = EnumName.memberName.rawValue

Step 3: Piece it together

var turtleSpeed = Speed.Slow.rawValue