O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The switch statement

The switch statement takes a value, compares it to several possible matches, and executes the appropriate block of code based on the first successful match. The switch statement is an alternative to using multiple else if statements when there could be several possible matches. The switch statement takes the following format:

switch value {  
  case match1: 
    block of code  
  case match2: 
    block of code 
  //as many cases as needed  
  default: 
    block of code 
} 

Unlike the switch statement in most other languages, in Swift it does not fall through to the next case statement; therefore, we do not need to use a break statement to prevent this fall through. This is another safety feature that has been built into Swift, as one of the most ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required