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!

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

Why is the code not correct? Am I missing something?

Please have look at my code. The question does not seem clear. I am not able to go ahead as the code seems to incorrect.

This is the question: "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 Compass.south 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 } let direction = Compass.south

enum Compass: Int{
    case north = 1
    case south = 2
    case east = 3
    case west = 4

let direction = Compass.south.rawValue

1 Answer

Kyle Johnson
Kyle Johnson
33,528 Points

This is what the code challenge is wanting:

let direction = Compass(rawValue: 2)

I think this code challenge is too confusing since the video tells us to do it the way you have above. Here's a link to apple's Enum page. You can search for raw values and there is a section all about it. The section you are looking for is called, Initializing from a Raw Value.