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

MINJI KIM
MINJI KIM
1,905 Points

what's wrong with the code?

code attached!

enums.swift
enum Compass: Int {
    case North = 1
    case South
    case East
    case West
}
let direction = 2
if let compass = Compass(rawValue: direction) {
    print(compass)
}

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey MINJI KIM,

You're close! You have your enum set up properly. You're just not doing exactly what the challenge asks.

The challenge wants you to initialize an instance of Compass with the value South by using the rawValue initializer. It then wants you to assign this instance to a constant named direction.

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

let direction = Compass(rawValue: 2)

By the way, it was good that you had the idea of using optional binding when using the rawValue initializer because it is a failable initializer!

Good Luck