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

I'm getting a weird error when I do: let turtleSpeed: Int = Speed (rawValue: 10)

Cannot convert from one type to another.

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

let turtleSpeed: Int = Speed (rawValue:10)

1 Answer

Hi there,

There's a couple of points there. The variable, turtleSpeed needs to be just that, a variable - so you need to declare it with var not let - that's a bit picky, though!!

The key thing, is what you're assigning to it. That requires dot notation to work. It should look like:

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

var turtleSpeed = Speed.Slow.rawValue

I hope that makes sense! Please ask if it doesn't.

Steve.

I see. I thought I could use the syntax that he showed:

let quarter = Coin (rawValue: 25)

And just create it from raw values. Instead of retrieving the raw value and then assigning it to the var turtleSpeed.

Hmmm it was a bit picky with let vs var. :P

Thanks a bunch!

Yeah - I remember being confused when I took this challenge as I was expecting to use the notation you used, rather than dot notation. Much frustration later, I figured it out which is why I remember it so clearly to help out now!