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

Will Van de Walle
Will Van de Walle
2,352 Points

Help on the code challenge?

I am a little confused as to why this code is not correct. The wording of the challenge confuses me a bit, and so that may be why. I plugged it into a playground; no errors and it seems to function as intended. Help?

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

let direction = Compass.South.rawValue
Will Van de Walle
Will Van de Walle
2,352 Points

Here is the 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 the enum value South of enum Compass to a constant named direction, by using a raw value with Compass' initializer method.

Will Van de Walle
Will Van de Walle
2,352 Points

Here is the 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 the enum value South of enum Compass to a constant named direction, by using a raw value with Compass' initializer method.

3 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Assign the enum value South of enum Compass to a constant named direction, by using a raw value with Compass' initializer method.

You are assigning the raw value of the enum member South to the constant direction. This way, direction is initialized as an Int with the value 2. Instead, you are asked assign the enum member South to direction. You could assign it directly, but the assignment asks you to initialize it with the raw value, that is 2.

enum Compass: Int{
    case North = 1
    case South = 2
    case East = 3
    case West = 4
}
let direction = Compass(rawValue: 2)

I did not test it as the challenge is not linked, but I hope this passes :)

Raphael Reiter
Raphael Reiter
6,820 Points

did Pasan Premaratne change the wording here? I have : Assign the enum value Compass.south to a constant named direction, by using a raw value with Compass' initializer method.

Thus the confusion.... arghhhh

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Raphael Reiter, you might have hit this issue as this course is for Swift 2.0. With Swift 3, enum members have to lowercase. So my answer would not be valid for Swift 3 :)

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

When an enum has raw values associated with it, it also has an init method that takes in the rawValue of the instance you wish to create. When instantiating a constant or variable (such as direction), that init method takes on the following form:

let direction = Compass(rawValue: /* INSERT RAW VALUE HERE */)

I hope this helps!

Ryan Harp
PLUS
Ryan Harp
Courses Plus Student 1,763 Points

Hello Will,

What Martin said was correct. The only thing that needs to be changed is that you need to put north, south, east, west in lower case!

Ryan