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.
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.
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)
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.
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.
Xcode 11 now appears to have a preference for this (checked in beta 5):
File > Preferences... > Text editing > Indentation
Swift
checkbox under Indent switch/case labels in:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With