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
Rafael Schwemmer
6,200 PointsData Type of Enum
In the Quiz we are asked what the data type of a constant is that was assigned an Enum.
I am confused why the data type of this constant:
let today = Day(rawValue: 1)
is not a "A Day". I thought when we define an Enum we define our own data type. Other possible answers were String, Int or Optional. I am sure it's not an Optional. So is it a String or an Int? And why not a Day?
Thanks!
4 Answers
Roberto Alicata
Courses Plus Student 39,959 PointsYes it can be very confusing. You must remember only that when you are using rawValue in the initialization you are obtaining an Optional
in this:
let today = Day(rawValue: 1)
today is of type "Day?" , or “optional Day.”
The answer can't be Day because it is "Day?" or in your example "squirrel?"
Rafael Schwemmer
6,200 PointsThanks, Roberto. So the correct answer for the quiz would have been "Optional" then? That's still not very clear to me. Isn't an optional String still a String? Isn't an optional Day still a type Day?
The example from the Apple docs state:
let possiblePlanet = Planet(rawValue: 7)
// possiblePlanet is of type Planet? and equals Planet.Uranus
"Not all possible Int values will find a matching planet, however. Because of this, the raw value initializer always returns an optional enumeration member. In the example above, possiblePlanet is of type Planet?, or “optional Planet.” "
So they say possiblePlanet is of type "Planet?" or "optional Planet". Then in the quiz the answer would be "optional Day". To then ask whether it's an "Optional" or a "Day" is strange and misleading. Or am I not getting it?
Roberto Alicata
Courses Plus Student 39,959 PointsOptional means that you can have a String (or Int or Date etc) in return or a nil value.
When you use the rawValue in a enum you are calling its initializer with the rawValue type but you are not sure that the init method returns an enumeration member because it can be a nil (so it is an optional)
Rafael Schwemmer
6,200 PointsThanks again for taking the time to trying to explain this. I think I understand the concept of an optional, more or less at least. What I don't understand is the Quiz question. We have an optional Day here and we are asked whether it is an optional or a Day. If I ask you whether an optional String is an Optioal or a String, what would you answer?
It seems to me this is like asking "We have a brown squirrel. What is it? a) brown b) a squirrel". I answered "squirrel" (Day) and it said "Bummer - the answer would have been brown"
Rafael Schwemmer
6,200 PointsYep, tricky stuff... Thanks. I'm new to this forum and strangely I can only select my own posts as "Best Answer" (which I won't do, of course)
Roberto Alicata
Courses Plus Student 39,959 PointsProbably because i only commented your question. Try to mark the answer below.
Roberto Alicata
Courses Plus Student 39,959 PointsRoberto Alicata
Courses Plus Student 39,959 PointsFrom the Apple developer Library : "If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value’s type (as a parameter called rawValue) and returns either an enumeration member or nil."
"Because of this, the raw value initializer always returns an optional enumeration member."