Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you Tweak Xcode's auto-indentation of Objective C Parameters?

Here's a snippet of code from my iOS app:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you like my hat?"
                                                    message:@"If so, please help spread the word by rating our app now?"
                                                   delegate:nil
                                          cancelButtonTitle:@"No Thanks"
                                          otherButtonTitles:@"Sure!", @"Maybe Later", nil
                          ];

Why does Xcode indent lines so bloody far? Being an old Perl, Ruby, and JavaScript monkey, I would be more inclined to indent it manually like this:

    UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:     @"Do you like my hat?"
        message:           @"If so, please help spread the word by rating our app now?"
        delegate:          nil
        cancelButtonTitle: @"No Thanks"
        otherButtonTitles: @"Sure!", @"Maybe Later", nil
    ];

So the parameter names and values are all left-aligned, and indented only one level (4 spaces for me). This uses far less screen real-estate, and it's less likely I'll have to deal with wrapping lines (which make the far-right indented stuff even trickier to read) on my MacBook Air.

However, I assume there's some reason Apple prefers the first method, and therefore has Xcode indent that way. Or is there?

So I have two question:

  • How do you prefer to indent method parameters in your Objective C code?
  • How do you tweak Xcode to format with your preferred indentation style?

Not trying to start a holy war here; I'm more curious about what tend to be the best practices among Objective-C programmers, how Xcode helps with those practices, and to find out if there is a good reason for the default way Xcode does it.


Update: Commenters and answers point out that the default formatting aligns the parameters at their colons. I should have remembered that before posting, since I have of course noticed it, and when the longest item isn't indented much, it can look pretty nice. But I find that, usually, the longest item is indented quite a lot. For example:

UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Do you like my hat?"
                                message:@"If so, please help spread the word by rating our app now?"
                               delegate:nil
                      cancelButtonTitle:@"No Thanks"
                      otherButtonTitles:@"Sure!", @"Maybe Later", nil
                      ];

Why is that? Even if I wanted them to align at the colons (and I've often hand-formatted them to do so), it seems silly to indent them so much. Why does it insist on indenting to the level of the opening colon? Why not just one indentation level, with the closing bracket out-dented to show the end of the indentation "block"? For example:

UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:@"Do you like my hat?"
              message:@"If so, please help spread the word by rating our app now?"
             delegate:nil
    cancelButtonTitle:@"No Thanks"
    otherButtonTitles:@"Sure!", @"Maybe Later", nil
];

This seems to be a better default than the Google style guide's line length recommendation, no?

Is there some way to tweak Xcode to format the parameters this way?

like image 634
theory Avatar asked Nov 24 '11 07:11

theory


2 Answers

1) The Objective-c indent convention is aligned at the colon sign when multiple parameters are given.

example

function:

- (void)functionWithOne:(NSString *)one two:(NSString *)two tree:(NSString *)three;

Has to be formatted to:

- (void)functionWithOne:(NSString *)one 
                    two:(NSString *)two 
                  three:(NSString *)three;

I came from Java, and it was really hard for me to get used to at first. But after trying it different a few times, this is really the nicest way.

2) I don't think (I am not sure) that you can change that in xcode. Apple's convention is pretty clear how it is.

EDIT: Invocations. I personaly start with the first parameter and align the rest like:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you like my hat?"
                                                message:@"If so, please help spread the word by rating our app now?"
                                               delegate:nil
                                      cancelButtonTitle:@"No Thanks"
                                      otherButtonTitles:@"Sure!", @"Maybe Later", nil];
like image 80
Justin Avatar answered Sep 18 '22 15:09

Justin


I'm not sure how to format it in Xcode, but two options for code formatting tools are:

  1. Use Uncristify, there's a GUI tool to configure it: UncrustifyX
  2. Use AppCode, the iOS/OSX IDE from Jetbrains - it has configurable and built in formatting. See screenshot.

AppCode code formatting tool

like image 38
Jasper Blues Avatar answered Sep 16 '22 15:09

Jasper Blues