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

Roderick Royce
Roderick Royce
1,236 Points

How do I assign the Int a number within the variable. The video demonstrated an if-let statement. Thanks.

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

var turtleSpeed = Speed(rawValue: 10)

// The variable above just isn't working the way I need it to for the challenge.
enum.swift
enum Speed: Int {
    case Slow = 10
    case Medium = 50
    case Fast = 100
}

var turtleSpeed = Speed(rawValue: 1) {
  yourSpeed(turtleSpeed)
}

2 Answers

Michael Hulet
Michael Hulet
47,913 Points

enums aren't constructed in the same way as structs or classes. In this case, I think you're trying to get the rawValue property of one of the possible enum cases. In the code snippet I'm about to give you, I'm going to assume you're looking for the number 10. Here's how to get the rawValue of an enum:

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

//You don't use object constructor syntax. Just assume that Swift already made an instance of that enum for you that you can use
var turtleSpeed = Speed.Slow.rawValue
yourSpeed(turtleSpeed)
Roderick Royce
Roderick Royce
1,236 Points

Thank you Michael. I also like your additional text to calculate "your speed" literally and make it yourSpeed. Thanks a ton!

Rod