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 2.0 Enumerations and Optionals Introduction to Enumerations The Problem with Primitives

Ryan Maneo
Ryan Maneo
4,342 Points

I was never told switch statements can have a return value...

Another omitted piece of crucial info in the lessons. I was never told this, and he never implicitly said it in the video either. He just assumed I knew it. Can someone please explain what switch statements can do? All I remember them doing is switching one string to another using a Dictionary. I was never given any more complex examples.

eyas shaaer
eyas shaaer
316 Points

the return is for the function not the switch, so the function well return different values depending on the conditions

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Well he sort of does imply it. In this video at about 1:17 he explains that switch statements are used instead of a long series of if / else if statements. And this is true. The implication here is that anything that can be inside the body of an if/else if statement can also be inside the body of a switch statement. This includes a return statement to return a value. You might consider taking a peek at the video again. It never hurts to have a refresher! Hope this helps! :sparkles:

Ryan Maneo
Ryan Maneo
4,342 Points

Thanks. Helped a lot :)

The function has to return a value if it is provided in the function declaration via ->. So, that is the reason why we use a return in a switch statement. With the same success we can use the return in any other control flow swift construct such as a loop (While, for) or a conditional statement (if statement). As soon as the return keyword in the code is reached the compiler stops and returns the obtained value. Hope this made things clearer.