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

Chase Maddox
PLUS
Chase Maddox
Courses Plus Student 5,461 Points

Enum Compass Code Challenge

Hi,

My code works fine in my Xcode compiler and returns the raw value "2". Why is this code challenge not accepting my answer?

Thanks, Chase

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

let direction = Compass.south.rawValue

5 Answers

Hi Chase!

Quick tip: To access the raw value

enumName(rawValue: defaultValue)

I hope this helps, but if you are still stuck don't hesitate to ask for more help.

All the best,

Mitch

I don't actually get it. Can you help me where should I put this code?

Batuhan,

Use the rawValue when assigning to the direction constant.

let direction = Compass(rawValue: 2)

This should return 'south'

I hope you can figure it out from here.

All the best,

Mitch

Thank you!

but why wouldn't we use the case name to define the value if we have declared a case name?

Loy Lee , there are many instances where the raw value is required instead of the case name.

As a quick example, image there was a method that took a string as an an argument. You could use the string raw value of an enum as this argument however you could not just use the enum case itself.

You're correct as well in that a lot of the time you do just use the case name and not the raw value.

It is just another tool that you will definitely see in iOS development.

If you have any other question please don't hesitate to reach out.

Also Loy, if you'd like to see my journey and where you could be before very soon check out

https://medium.com/@mitch_little/latest

I hope it brings you some value and motivation!

Wishing you all the best and happy coding,

Mitch

Daniel Walkowski
Daniel Walkowski
549 Points

A general issue I have with the Treehouse interpreter is that it seems to get confused very easily. I never code in the Treehouse interpreter, only in a playground. If I accidentally do edit Treehouse, I always refresh the window before submitting.

The Treehouse compiler accepts very specific solutions and can provide the wrong help at times. As you say, it is best to code in Xcode and just copy and paste it in. It avoids the headaches of the Treehouse compiler and also makes for good practice considering it is the IDE you use to build your applications.

Not sure if it's me but I always get confused with he instructions of the challenges. The videos seem to teach you many many ways to code, and the challenges only seem to accept a very specific answer. Which is not alway that intuitive. I waste a lot of time trying to intemperate the challenge questions.

Loy Lee, that is very true a lot of the time. Sometimes the questions are very specific.

I'd recommend writing your answers in a Playground.

Firstly, it is great practice with Xcode and provides you with auto complete.

Secondly, if your code compiles in the Playground and not in the Treehouse compiler you know it is just the Treehouse compiler that is being picky and not your code that is wrong. Then it is just a case of realising that in code there are often many different solutions and you have the skills to solve problems, congrats! You then just have to manipulate it to pass the Treehouse code challenge.

I answered your question above as well.

I hope I have helped you.

Again, feel free to reach out!!

Mitch

Thanks for the reply Mitch Little !