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 Enums and Structs Enums Review Enums

Conor Carey
Conor Carey
3,775 Points

I don't understand why the datatype answer is an optional rather than a day

One of the review enum questions in the swift training course asks for the datatype of a piece of data (I don't remember the specifics), but I do remember being confused when the answer said it was an optional rather than a day. Can someone please clarify this?

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Conor!

You should probably review this video from a couple lessons ago on enums and raw values.

In the quiz, the Day is being initialized with a raw value. Something like...

let today = Day(rawValue: 2)

Because Swift can't be certain you'll provide a valid raw value (we could have written rawValue: 238), when you initialize an instance of Day in this way, the datatype is optional. You can use if let, or the ! operator to get around this.

Hope this helps! As I said, I recommend watching the video again, since Amit goes into more detail, and you can follow along in Xcode.

-Greg