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 Swift 2.0 Enumerations and Optionals Objects and Optionals Enums with Raw Values

Enums 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.

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

let direction = Compass.South.rawValue

7 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey 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. :)

OMG, thank you! It's not worded very well, so it's confusing!

Dominiq Martinez
Dominiq Martinez
1,949 Points

Where 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?

Arman Arutyunov
Arman Arutyunov
21,900 Points

Andrew 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?

Having 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

Many 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
Steven Barkley
8,924 Points

I don't like to complain but I agree wholeheartedly with this statement.

Andrew Timosca
Andrew Timosca
6,354 Points

Jason, 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
Andrew Timosca
6,354 Points
Compass(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
Jason Anders
Treehouse Moderator 145,860 Points

Hey 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.

:dizzy:

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

let direction = Compass(rawValue: 2) Had me going to but this is an initializer challenge so it makes since.

explicitly clear question for beginner please

CJ PHUA
CJ PHUA
6,729 Points

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

let direction = Compass(rawValue: 2)

doesn't work