Opening {‘s on a new line in XCode
Posted by Richard Le Mesurier in Coding Tricks, How-to, iOS
<rant> I find code easier to read when there is lots of space in it.
Spaces before + in equations, spaces after : , blank lines between blocks of code etc. I sometimes find myself putting 2 blank lines in between methods; I always try to stick to Joel’s conventions, and really like having as few lines of code in each method as possible.
Like we use punctuation to break up paragraphs, sentences, thoughts; how one takes breaths when speaking, to break up a monologue.
It could be because I’ve had the pleasure of working with ASM in good old DOS days. Maybe it’s a sign of my age (over 30! a veritable Highlander here at Cobi)… Whatever the reason, I just find it easier… </rant>
So it should be no surprise that I like my code blocks to start with a { on a line of its own:
for (int i=0; i++; i < 3)
{ // <- like this
...
}
In java & eclipse, this was easy; but entering the iWorld of Xcode I started to feel very disenchanted. I could no longer get the IDE to format my code the “right” way (apologies to all those who like their brackets where they are – it really is just an opinion).
After lots of searching (fighting, tears) I came across the following: just copy’n'paste this into terminal to “fix” your curlies:
defaults write com.apple.Xcode XCCodeSenseFormattingOptions -dict BlockSeparator "\\n" PreMethodDeclSpacing ""
(oh, restart XCode…)
This tip (and others) from the friends at StackOverflow at:
- Xcode-adjusting indentation of auto-generated braces
- What are those little xcode tips & tricks you wish you knew about 2 years ago?
And an explanation of the Xcode defaults can be found at:
If you, like me, feel there really IS a difference between “right” and “wrong”, then this might just save you some frustration.
(Standard T’s & C’s apply: HTH, YMMV)



Good post, agree with most of it and love the terminal line to change where Xcode puts the {. However your { bracket in the topic just compounded my unresolved feelings gained from xkcd earlier today.
Thinking about it I just added 2 more unmatched brackets.