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 trialMoath Maharmah
3,613 PointsEnums RAW VALUES Why we use it ?
I'm confused a little bit about Enums Raw values
why do we use it ?
2 Answers
Wouter Janson
1,545 PointsYou can you the raw value to calculate with the enums. If you have an enumeration with all the planet in our solar system, you can give them a raw value type for the order in which they appear in our solar system.
So we could use logic with them:
enum Planet: Int {
case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
//Auto numbering is on by default
}
let positionToFind = 9
if let somePlanet = Planet(rawValue: positionToFind) {
switch somePlanet {
case .Earth:
print("Mostly harmless")
default:
print("Not a safe place for humans")
}
} else {
print("There isn't a planet at position \(positionToFind)")
}
This wil print "There isn't a planet at position 9" because there is no enum value with the raw value of 9. If positionToFind was 3, it would have printed: "Mostly harmless" because earth has the raw value of three
Moath Maharmah
3,613 PointsThank you !