Opening {‘s on a new line in XCode tagged: ,

Opening {‘s on a new line in XCode

Posted by 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:

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)