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 Enumerations and Optionals in Swift Objects and Optionals Enums with Raw Values

Tyler Dotson
PLUS
Tyler Dotson
Courses Plus Student 1,740 Points

why doesn't this work?

i followed the example from the video and i dont understand why its wrong.

enums.swift
enum Compass: Int {
    case north = 3
    case south = 4
    case east = 5
    case west = 6
}

if let direction = Comapass(rawValue: south) else {
    return nil
}

2 Answers

If you change Comapass to Compass that's a start :wink:

You can chase the errors down by using the Preview button.

You don't need an if let just use let. Then use the rawValue associated with south - that's 4.

That gives you:

enum Compass: Int {
    case north = 3
    case south = 4
    case east = 5
    case west = 6
}

let direction = Compass(rawValue: 4) 

Make sense?

Steve.

As Steve says, you don't need to use an "if let", just use "let". Also remember that the raw value is the value AFTER the equals sign, not after "case". Hence the raw value for south is "4", not "south". Therefore, as Steve says, your constant should be declared as:

let direction = Compass(rawValue: 4)