Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode - changing the indentation rules for switch statements

Tags:

When I write a Swift switch statement it indents the code like this:

switch foo { case 1:     // stuff happens default:     // other stuff happens } 

I want it to indent like this:

switch foo {     case 1:         // stuff happens     default:         // other stuff happens } 

Is there any way to do this? All the questions I can find on the topic either point to plugins (which no longer work in the latest version of Xcode) or discuss which way is "correct" rather than offer a way to change it.

like image 551
John Montgomery Avatar asked Feb 21 '17 20:02

John Montgomery


People also ask

How do I fix indentation in Swift?

Xcode tip #21: If you've copied code from elsewhere, such as Hacking with Swift or Stack Overflow, there's a good chance you'll paste in something with incorrect indentation. Xcode can fix this with one shortcut: select the code you want to fix, then press Ctrl+I to reindent it.

How do I indent all codes in Xcode?

Select the entire code and press control + I on mac to format your code. Select the code and press: ⌘ + ] for right move (indent) ⌘ + [ for left move (un-indent)

Do you need break in switch statement Swift?

Although break isn't required in Swift, you can use a break statement to match and ignore a particular case or to break out of a matched case before that case has completed its execution. For details, see Break in a Switch Statement. The body of each case must contain at least one executable statement.

What is a switch statement in Swift?

The switch statement in Swift lets you inspect a value and match it with a number of cases. It's particularly effective for taking concise decisions based on one variable that can contain a number of possible values. Using the switch statement often results in more concise code that's easier to read.


2 Answers

Xcode 11 now appears to have a preference for this (checked in beta 5):

  • Go to File > Preferences... > Text editing > Indentation
  • Check the Swift checkbox under Indent switch/case labels in:
  • Party!
like image 161
Jaysen Marais Avatar answered Oct 25 '22 05:10

Jaysen Marais


As of Xcode 9, there is an indentation settings page in Preferences/Text Editing/Indentation, but this includes no options for advanced indentation for each statement.

In Xcode 9, one may now re-indent code by marking it, pressing Tab or Tab + Shift

like image 21
Andreas detests censorship Avatar answered Oct 25 '22 05:10

Andreas detests censorship