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

Works on Xcode but not here. Why?

The question is: Let's get in some practice creating enums with raw values. Declare an enum named Compass with a raw value of type Int.

Give the enum 4 members: North, South, East and West and assign them default raw values.

Assign the enum value South of enum Compass to a constant named direction, by using a raw value with Compass' initializer method.

My code:

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

if let direction = Compass(rawValue: 2) {

    print(direction)
}

my code works in Xcode but not on treehouse. Can someone explain what I'm doing wrong. Thanks.

I saw in other posts that using the below code works:

let direction = Compass(rawValue: 2)

Can someone explain the difference between my code and the answer? Thanks.

Can you link the question please?

2 Answers

"Assign the enum value South of enum Compass to a constant named direction, by using a raw value with Compass' initializer method."

There was no instruction to do a if-statement...just an assignment of the second value in your enum. Just do the stuff they want from you or sometimes challenges wont work

Yer all you should be doing is this,

enum Compass: Int {

  case North = 1
  case South
  case East
  case West
}
let direction = Compass(rawValue: 2)

Don't just copy the code, understand it though. So you are setting up an enum called Compass of type int. Adding the cases (only need to do the first one, swift is smart enough to number the rest).

Then you assign a constant (let) named direction to the rawValue of 2 (south) of Compass.

does not pass

he specifically says: Assign the enum value Compass.south to a constant named direction, by using a raw value with Compass' initializer method.