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 trialMelissa Allgeier
13,564 PointsEnums and Optionals, Enums with Raw Values Challenge
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 an enum value to type South to a constant named direction, by using a raw value with its initializer method.
I'm not even 100% sure what they are asking for. This code doesn't work and I'm not even sure what else to try.
enum Compass: Int {
case North = 1
case South
case East
case West
}
let direction = Compass.South.rawValue
7 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHey Melissa,
The way the question is worded, it takes not 'over-thinking' it to solve it. I admit, I overthought this one and it kind of stumped me for a bit. Basically, the task wants you to assign the the value (South) to the constant using the rawValue for South.
South has an assigned rawValue of 2, so to assign that to the constant, you need to do this.
let direction = Compass(rawValue: 2)
Hope this helps. :)
Arman Arutyunov
21,900 PointsAndrew I agree. Lots of iOS challenges in last few courses became like this. They ask to do things that are not covered in videos and it's very disappointing personally for me that I can't pass it without researching the discussions. I thought that the main purpose of challenges is to try out new abstract theory on the certain practical situation. But what I see now is that I can't complete it simply because theory and practice are often not quite related. It would be great if they started considering this problem. Does anybody know where can I write this critique to actually see the improvements?
Thomas Léger
7,021 PointsHaving to search a little by yourself won't hurt, on the contrary :-) And thanks Jason. I was bent on using "if let" and you opened my eyes :p
kelake
4,223 PointsMany of the challenges are worded confusingly.
Of course doing some outside reading or research is par for the course, but often I find myself relying on outside material far more than what is provided here.
Steven Barkley
8,924 PointsI don't like to complain but I agree wholeheartedly with this statement.
Andrew Timosca
6,354 PointsJason, I just rewatched. Pasan shows it the way Melissa posted above. He makes no mention of using the code I mentioned in my first post.
Andrew Timosca
6,354 PointsCompass(rawValue: 2)
was most definitely not covered in the previous videos. How would I have known to do that? Maybe I missed something?
Jason Anders
Treehouse Moderator 145,860 PointsHey Andrew,
This is actually covered in the first video of the course ("Enumerations With Raw Values").
Pasan covers the example that is directly related to the challenge question starting at approximately 4:45 in the video.
Ingo Ngoyama
4,882 Pointslet direction = Compass(rawValue: 2) Had me going to but this is an initializer challenge so it makes since.
Tola Veng
23,749 Pointsexplicitly clear question for beginner please
CJ PHUA
6,729 Pointsenum Compass: Int { case North = 1 case South case East case West }
let direction = Compass(rawValue: 2)
doesn't work
Melissa Allgeier
13,564 PointsMelissa Allgeier
13,564 PointsOMG, thank you! It's not worded very well, so it's confusing!
Dominiq Martinez
1,949 PointsDominiq Martinez
1,949 PointsWhere did you get the assigned raw value of 2 from? I also am confused as to where the 1 came from for north. Im assuming the placement in sequential order in the list of enums is the idea but I'm not sure?