Search code examples
xcodesettingsxcode4.5indentationcurly-braces

Is there any setting in Xcode to have all autocomplete code follow a different curly bracket style?


Per Wikipedia, Xcode defaults to using the K&R brace/indentation style. It looks like this:

- (void) someFunction {
    if (condition) {
        // do this
    } else
        // do that;
    }
}

I've grown up with a strong preference for Allman style, which looks like this:

- (void) someFunction
{
    if (condition)
    {
        // do this
    }
    else
    {
        // do that
    }
}

It's not too much of a chore just to return the bracket of a code-completed statement to the next line, but if I could tell Xcode to do this for me, it would save me a lot of meticulous click-and-return'ing.


Solution

  • I don't think you can remove the K&R braced code snippets, but you could simply create your own Allman-style snippets and add them to the Xcode "Code Snippet Library". It's a cumbersome solution, but it works.

    So, when writing your snippets, you can use the <# and #> to delimit your expressions. Thus:

    if (<#condition#>)
    {
        <#statements-if-true#>
    }
    else
    {
        <#statements-if-false#>
    }
    

    Then drag and drop that onto your snippet library:

    enter image description here

    And give it a meaningful name and shortcut:

    enter image description here

    Now when you start typing, you'll leverage the new snippet:

    enter image description here

    Go ahead and give your snippet a unique completion shortcut if you want to make disambiguation even easier.

    There are more effective uses of the snippet library, but it can be used to address your problem.