How to Make Xcode Put Starting Brace on New Line in Swift

Default opening curly braces in Xcode

By default Xcode is missing a good, customisable code formatting settings. You can find some external plugins that are mentioned here. Personally I haven't tried them. So in short, Xcode is missing that feature.

As a side tip I can mention that AppCode has a quite good way to format code in the way you want and the keyboard shortcuts for auto formatting code and optimising imports.

Adding lines to parenthesis in Xcode as others code editors

how can I add lines to my code on Xcode as other code editors like the image below?

You can't, because Xcode doesn't render code that way. However, those vertical lines are often used to indicate which lines will be collapsed or folded, and Xcode does support code folding:

sample code with folding bars

The vertical gray bars next just to the right of the line numbers are Xcode's way of showing how code is nested. If you place the cursor over one of those bars, Xcode highlights the beginning and ending braces. Clicking on a bar will fold the corresponding block down to a single line with ... between the braces. This is similar to what the vertical indentation lines do in some other editors.

Xcode 10: Auto insert closing braces bug

The problem was fixed by Apple in Xcode 10.1 (10B61)

Xcode: Adjusting indentation of auto-generated braces?

For Xcode 3.x, you can use the following:
If you open up a terminal session and enter

defaults write com.apple.Xcode XCCodeSenseFormattingOptions '{ "BlockSeparator" = "\n" ; }'

it will start new blocks of code on a new line. Note that you will have to restart XCode if you have it opened in order for the new defaults to be read and used.

For Xcode 4.x to 6.x (current) I suggest you use Snippet Edit as a handy tool for editing the snippets that define the Xcode autocompletion. At the current time it's free and works great.

Xcode 9 expand/collapse braces/brackets {} side bar missing

Updates in Xcode 10:

Xcode 10 has increased support for code folding, including:

  1. A new code folding ribbon showing all of the multi-line foldable blocks of code in the editor
  2. A new style for folded code in the editor that allows you to edit lines with folded code
  3. Support for folding any block of code enclosed in curly braces
  4. Support for folding blocks of code from the folding ribbon, from structured selection, or from the

Menubar ► Editor ► Code Folding ► Fold menu item

Sample Image

Look at this snapshot:

Sample Image


Xcode 9

Code folding was disabled in Xcode 9 beta 1, which is working now, in Xcode 9 Beta5 according to beta release note: Resolved in Xcode 9 beta 5 – IDE

Here is how:

  1. Press and hold (command) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
  2. Keep (hold) (command) button in pressed condition and click on highlighted area. It will enable quick menu popover window with Fold option.
  3. Select Fold from menu list. It will fold your code and shows 3 dots, folding/covering entire block.
  4. Now, to again unfold your code block, release (command) button and click on 3 dots folding a block.

For easy understanding, look at this snapshot:

Sample Image


It's all keyboard short cuts are also working.

Fold                          ⌥ ⌘ ←      option + command + left arrow
Unfold ⌥ ⌘ → option + command + right arrow
Unfold All ⌥ U option + U
Fold Methods & Functions ⌥ ⌘ ↑ option + command + up arrow
Unfold Methods & Functions ⌥ ⌘ ↓ option + command + down arrow
Fold Comment Blocks ⌃ ⇧ ⌘ ↑ control + shift + command + up
Unfold Comment Blocks ⌃ ⇧ ⌘ ↓ control + shift + command + down
Focus Follows Selection ⌃ ⌥ ⌘ F control + option + command + F
Fold All ⌘ ⌥ ⇧ ← command + option + shift + left
Unfold All ⌘ ⌥ ⇧ → command + option + shift + left


Code folding options from Xcode Menu:

Menubar ▶ Editor ▶ Code Folding ▶ "Here is list of code folding options"

Here is ref snapshot:

Sample Image

Same options from Xcode Short-cut list:

Menubar ▶ Xcode ▶ Preferences ▶ Key Bindings ▶ "Here is list of code folding short-keys"

Sample Image



Related Topics



Leave a reply



Submit