Switch Cases in Swift

Farhan Syed
iOS App Development
1 min readApr 9, 2017

--

You’ve most likely heard of an if statement, think of switch cases to be the more advanced form of a if.

To start a switch statement you tell Swift what variable you want to run things by, then give the list of possible cases.

Swift will find the case that matches your code first and then execute it then exit the switch.

Here’s a simple example:

Even if you think you know your cases will always be executed, you need to have the default: to fall back on.

Multiple Cases

Let’s add more cases and utilize some more types of conditions.

We have a number like last time, though this time we will check for a range of numbers in each case.

Like so:

To check for a range you simply use three periods like so:

1…100

Nothing has changed really since the first one.

We just added more cases along with checking for a range of numbers just to demonstrate what you can do in a case.

That’s pretty much it!

--

--