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
Alia Khan
3,733 PointsEnums and switch Statements in swift
Hi
I've just been playing around with code in playground and have found that I don't get that compiler help when using enums and switch statements unless I use a function as well. why is that? The apple documentation examples all use the dot notation when using switch statements and enums and my playground isn't doing that. is this just me doing something wrong?
Jennifer Cordell
enum Day { case Monday case Tuesday case Wednesday case Thursday case Friday case Saturday case Sunday }
var favouriteDay: Day = .Friday
switch favouriteDay { case .fr
}
here is an example @Moritz Lang theres an error and no compiler help so I've had to type the day myself
3 Answers
Moritz Lang
25,909 PointsHi, can you please show us some code examples of yours? :]
Moritz Lang
25,909 PointsHi Alia, thanks for posting the code! :) the first mistake I saw in your Snippet was writing mutiple cases in one line. Don't get me wrong on this, you can actually write multiple cases in one line, but you have to separate them using commas, like so:
enum Day {
case monday, tuesday, wednesday, thursday, friday, saturday, sunday
}
I would personally declare favouriteDay like that:
var favoriteDay = Day.friday
because it's easier to read and understand.
Last but not least your Switch-Statement:
Where does .fr come from?
If you'd like to switch over favoriteDay you could write something like this:
switch favoriteDay {
case .friday:
print("It's Friday!!")
default:
print("It's not Friday.")
}
I hope this helps :)
Alia Khan
3,733 PointsHi sorry my code hasn't pasted correctly for some reason and unfortunately I'm still having the same problem the switch statement does not provide any compiler help so its the same as suing string values unless i use a function
Moritz Lang
25,909 PointsDid you use my code written above? If so you should get auto completion on the switch statement. Maybe you simply have to recompile your code.
Alia Khan
3,733 PointsHi i did exactly as you said. I even re wrote the code in a new playground but I'm not getting that auto completion help within a switch statement . Im getting the autocompletion outside the switch statement but not inside when i use the case keyword. Any ideas what may be causing this? my macbook air is brand new got it a month ago
Alia Khan
3,733 PointsAlia Khan
3,733 PointsMoritz Lang