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 trialWoongshik Choi
42,387 PointsWhat'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
Evan Demaris
64,262 PointsHi 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)
rh12
4,407 PointsHi 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
9,951 PointsThis 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
Woongshik Choi
42,387 PointsWoongshik Choi
42,387 PointsThanks 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.
Evan Demaris
64,262 PointsEvan Demaris
64,262 PointsIt 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.