How can I configure Xcode to put '{' where I want it in generated files
Asked Answered
O

4

11

I know this is a fairly contentious issue amongst programmers, but when developing I like my IDE to position the opening curly bracket underneath the method/interface/control declaration, for illustrative purposes: -

This is how Xcode automatically generates skeleton methods with the { at the end: -

-(void) isTrue:(BOOL)input {
    if(input) {
        return YES;
    }
    else {
        return NO;
    }
}

This is how I like to lay out my code (which I believe is called the Allman style): -

-(void) isTrue:(BOOL)input 
{
    if(input) 
    {
        return YES;
    }
    else 
    {
        return NO;
    }
}

I'm just wondering if there's any configuration switch in Xcode to enable this style of development? It's really annoying when typing out if/else statements as it tends to auto-complete the else clause with the { at the end of the line which just looks silly if you like developing with them underneath.

Or am I being unreasonable? Is Objective-C supposed to adhere to a standard defined by Apple?

Otiose answered 10/4, 2010 at 13:50 Comment(0)
D
9

Take a look at:

Xcode: Adjusting indentation of auto-generated braces?

Apple Xcode User Defaults

XCCodeSenseFormattingOptions = {
  BlockSeparator = "\\n";
  PreMethodDeclSpacing = "";
};

This should at least solve your problem after if, for, or while statements.

Dermatoid answered 10/4, 2010 at 14:11 Comment(0)
A
2

After digesting the helpful information from WhirlWind above (thanks), the resulting snippet (just cut and paste into terminal) is:

defaults write com.apple.Xcode XCCodeSenseFormattingOptions -dict BlockSeparator "\\n" PreMethodDeclSpacing ""

Stupid backslash quoting. When typed at the terminal, there should be TWO exactly TWO backslashes in the block separator.

Apnea answered 10/4, 2010 at 15:31 Comment(1)
Yeah I got a bit confused about that, I did something similar to the above but used the -dict-add flag for each key/value pairOtiose
F
0

Even with those settings, it does not appear to work with the templates. If you set this and then type "init" in a .m file you get:

- (id)init
{
  self = [super init];
  if (self) {
    <#initializations#>
  }
  return self;
}

Note the "if (self) {" line.

Foeman answered 21/3, 2012 at 21:35 Comment(0)
A
0

I believe that "defaults write com.apple.Xcode" doesn't work on the latest versions of Xcode (7.x)

Here are the solutions I know:

  1. Snippet Edit -- this little program will allow to edit default Xcode's code snippets. So, you will be able to open braces from new line in your if, for, while, etc. However, this doesn't allow to change the block indentation.

  2. Uncrustify -- this might solve your problem too, but it doesn't look like being easy to set up. And it only formats the code after it is already written, instead of formatting 'on the go'.

Allow answered 31/1, 2016 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.