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:
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:
- A new code folding ribbon showing all of the multi-line foldable blocks of code in the editor
- A new style for folded code in the editor that allows you to edit lines with folded code
- Support for folding any block of code enclosed in curly braces
- Support for folding blocks of code from the folding ribbon, from structured selection, or from the
Menubar ► Editor ► Code Folding ► Fold menu item
Look at this snapshot:
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:
- Press and hold ⌘ (command) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
- Keep (hold) ⌘ (command) button in pressed condition and click on highlighted area. It will enable quick menu popover window with
Fold
option. - Select
Fold
from menu list. It will fold your code and shows 3 dots, folding/covering entire block. - 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:
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:
Same options from Xcode Short-cut list:
Menubar ▶ Xcode ▶ Preferences ▶ Key Bindings ▶ "Here is list of code folding short-keys"
Related Topics
Using Codable to Encode/Decode from Strings to Ints with a Function in Between
Saving Nsimage in Different Formats Locally
Get the String Up to a Specific Character
Moving Skspritenode to Location of the Touch
Replace Exactly One Pixel in an Image and Put It in Another Image via Swift
[_Nscftimer Copywithzone:]: Unrecognized Selector Sent to Instance
Type a Requires That Type B Be a Class Type Swift 4
Overriding Static Vars in Subclasses Swift 1.2
Swift: How to Create a Predicate with an Int Value
Setting Nsunderlinestyle Causes Unrecogognized Selector Exception
Load Image from Url on Watchkit
iOS Firebase: Firauthuidelegate.Authui Not Being Called
Passing a Variable Through a Segue? Xcode 8 Swift 3
Obtain Nsurl from Uiimagepickercontroller
Swift: Generic Overloads, Definition of "More Specialized"