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

Steffen Alexander Ness
Steffen Alexander Ness
11,076 Points

Enum rawValue code does not pass

I need some help figuring out why this code does not pass.

enums.swift
enum Compass {
    case north = 1
  case south = 2
  case east = 3
  case west = 4
}

let direction = Compass.south(rawValue: 2)
Steffen Alexander Ness
Steffen Alexander Ness
11,076 Points

I get this error: "Bummer: Make sure you're creating an enum value using the raw value initializer and assigning the result to direction"

1 Answer

Clark Reilly
Clark Reilly
6,204 Points

The enum needs to be assigned a raw value type of Int

enum Compass: Int {
// cases are fine
}

Also

Compass.south doesn't have an initializer, you need to call it on Compass:

let direction = Compass(rawValue: 2)