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

Lana Wong
Lana Wong
3,968 Points

I do not understand why my enum initializer isn't working?

In this challenge, I did what Pasan did and it worked on Xcode too. I do not understand what is wrong with my code.

enums.swift
enum Compass: Int {
  case north = 1
  case south = 2
  case east = 3
  case west = 4
}
 let value = 2
 if let direction = Compass(rawValue: value){
 print(direction)
 }

1 Answer

ceriannejackson
ceriannejackson
23,759 Points

There's no need to use if let as you know you can initialise the enum with a raw value of 2 and you're not using direction afterwards. So, you can simplify your code to:

let direction = Compass(rawValue: 2)