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 Basics (retired) Control Flow Switch Statement

Hudson Levy
Hudson Levy
512 Points

If you have every "case" accounted for, why do you need a "default"?

In other words, if the "default" is never tapped then why is it required to be there?

2 Answers

Hey Hudson.

Default cases are usually there to be used if one of the specific pre-defined cases isn't selected by the program. Even if you are pretty sure that incorrect input can't be entered it is still best to include one just incase none of the other cases are picked.

Have fun programming,

-Luke

Patrick Cooney
Patrick Cooney
12,216 Points

Swift aims to be a "safe" language. You may know that all the cases are covered but the compiler doesn't and all the compiler cares about is making sure your code minimizes the potential of crashes. If you use other languages at all you might notice that missing defaults can be a common cause for an issue. Swift does away with this by forcing you to put a default case whether you think you need it or not.