Automatically Indent Switch Statement Cases in Xcode

One of the formatting defaults in Xcode you may want to change is the lack of indenting on switch statements. By default, this is how the parts line up:

switch preferredStatusBarStyle {
case .darkContent:
    handleDarkContentStatusBar()
case .lightContent:
    handleLightkContentStatusBar()
case .default:
    handleDefaultStatusBar()
}

There is a setting in Xcode preferences that will add an indentation before the cases, which can make switch blocks easier to read. To enable this, go to:

  1. Xcode menu
  2. Preferences
  3. Text Editing tab
  4. Indentation sub-tab.

Xcode setting for switch case indentation

Xcode will now automatically indent like so:

switch preferredStatusBarStyle {
    case .darkContent:
        handleDarkContentStatusBar()
    case .lightContent:
        handleLightkContentStatusBar()
    case .default:
        handleDefaultStatusBar()
}

Tags: