Syntax

Swift Switch Syntax

In Swift, switch: matches a value against cases — switch n { case 1: ... default: ... }

Quick Answer

Syntax
switch n { case 1: ... default: ... }
SwiftSyntax

Switch

Code example
switch n { case 1: ... default: ... }

Matches a value against cases.

Learn More

How to switch in Swift

  1. Open your Swift file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Swift Switch Syntax

Use switch n { case 1: ... default: ... }.

Related Syntax References

Frequently asked questions

What is the Swift syntax for switch?

Use this pattern: switch n { case 1: ... default: ... }

Can I copy this Swift code example?

Yes. Use Copy code and adapt the example to your program.

Is this Swift syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify