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

Richard Pitts
PLUS
Richard Pitts
Courses Plus Student 1,243 Points

Thought this was correct?

Thought this was correct?

enums.swift
enum Compass: Int {
  case south
  case east
  case north
  case west
  }

  let direction = Compass.south.rawValue

1 Answer

David Papandrew
David Papandrew
8,386 Points

Hi Richard,

*UPDATED ANSWER* Whoops, as Raphael pointed out, I made a mistake in my response.

Here is the corrected code:

enum Compass: Int {
    case north = 1
    case south
    case east
    case west
}

let direction = Compass(rawValue: 2)

*ORIGINAL RESPONSE*

The Compass enum is missing a couple of items:

1) The enum's raw value should be String not Int

2) The enum cases should each have raw values assigned

Once you have set these items up, you can instantiate the enum object properly. See the code below:

enum Compass: String {
    case north = "North"
    case south = "South"
    case east = "East"
    case west = "West"
}

let direction = Compass(rawValue: "South")
Raphael Reiter
Raphael Reiter
6,820 Points

Hi David Papandrew , I dont understand. Pasan says: Let's get in some practice creating enums with raw values. Declare an enum named Compass with a raw value of type Int.

he also say to use : Assign the enum value Compass.south to a constant named direction, by using a raw value with Compass' initializer method.

David Papandrew
David Papandrew
8,386 Points

You are right Raphael, I've updated the response above.