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
Farhan Hussain
13,928 PointsWorks 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.
Andrew Taylor
11,500 PointsCan you link the question please?
Farhan Hussain
13,928 Points2 Answers
tobiaskrause
9,160 Points"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
Andrew Taylor
11,500 PointsYer 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.
Mackenzy Douyon
iOS Development Techdegree Student 2,913 Pointsdoes not pass
Raphael Reiter
6,820 Pointshe specifically says: Assign the enum value Compass.south to a constant named direction, by using a raw value with Compass' initializer method.
Farhan Hussain
13,928 PointsFarhan Hussain
13,928 PointsI 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.