Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Enumerations and Optionals Objects and Optionals Enums with Raw Values

Woongshik Choi
Woongshik Choi
42,387 Points

What's wrong on my code?

I don't know what's wrong on my codes below,

enum Compass: Int {

    case North = 1
    case South
    case East
    case West

}

let Direction = Compass(rawValue: 2)

if let Direction = Compass(rawValue: 2) {
    print("\(Direction)")
}

2 Answers

Hi Woongshik,

it looks like your code is missing commas after the cases; if you add them, it should work.

enum Compass: Int{
  case North = 1,
  South,
  East,
  West
}

let direction = Compass(rawValue: 2)
Woongshik Choi
Woongshik Choi
42,387 Points

Thanks Evan,

By the way I wonder why multiple case clauses don't work. I followed your code which is one unified case clause for the enum members.

It appears that the reason my code worked was actually just the lowercase direction; the documentation on enumeration in swift includes this compass example specifically, without commas.

Hi Woongshik,

There doesn't seem to be anything wrong with the code in your original post. You do not necessarily need the commas after each case. Without knowing the exact error you received, my guess is that the online code editor is having an issue with declaration of the "direction" variable. I think there may be a typo in the instructions for the task. The online code editor seems to be expecting a lowercase "d" for "direction", instead of the uppercase "D" that you used (which is what is be asked for in the instructions).

There was another post in the community that raised a similar issue. see here

-- Ryan

cameron swenson
cameron swenson
9,951 Points

This exactly. Ran the code like Woongshik had it with a capital D which gave an error then swapped it to lowercase and it worked perfectly