Fix code indentation in Xcode

Viewed 108232

Once I start editing my code and adding for loops or if then statements my code indentation is whacked because the previous code maintains its former indentation instead of adjusting automatically.

In Visual Studio you can highlight code and select 'format selection' to fix these issues - is there anything similar in Xcode?

10 Answers

You can do this automatically in Xcode 11

To quickly re-indent the whole file:

+a

+x

+v

Edit: No longer working in Xcode 11

You can do it in different ways, but these two are the easiest

  • select text ( +a) :
  1. Using shortcut -> (control)^ + i

  2. Click on "Editor" -> Click on "Structure" -> Click on "Re-indent"

Sometimes setting Xcode global preferences is not enough for ^ + i to do its job accordingly. The reason is that project settings override global ones. You can set indentations for a concrete project or even a file. Select project (or file) in the Navigation Pane. Then in File Inspector go to Text Settings section and set desired number of tabs or spaces for indents.

Related