How do you configure Xcode auto-indentation for Objective-C parameters?

Here is 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 are Xcode indentation lines so bloody? Being an old monkey of Perl, Ruby and JavaScript, I would be more inclined to step back from it manually:

  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 ]; 

Thus, the names and values โ€‹โ€‹of the parameters are left-aligned and have only one level (4 spaces for me). This uses far less screen real estate, and, less likely, I have to deal with line wraps (which make it completely believable, even more difficult to read) on my MacBook Air.

However, I assume that Apple for some reason prefers the first method, and therefore has such an Xcode indent. Or is there?

So, I have two questions:

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

Not trying to start a holy war here; I am more curious that, as a rule, the best methods are among Objective-C programmers, as Xcode helps with these practices, and find out if there is a good reason for the default method that Xcode does.


Update:. Commentary and answers indicate that formatting by default aligns parameters in its columns. I had to remember that before publication, since I, of course, noticed this, and when the longest element does not have a large segment, it can look pretty pretty. But I find that, as a rule, the longest element is indented quite a lot. For instance:

 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? Even if I wanted them to be aligned with colons (and I often manually formatted them for this), it seems silly to backtrack from them so much. Why does he insist on indentation to the level of the colon? Why not only one level of indentation, but the closing bracket is pushed out to show the end of the โ€œblockโ€ of the indentation? For instance:

 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 ]; 

Looks like this is a better default than google style guideline length recommendation , no?

Is there a way to configure Xcode to format the parameters this way?

+6
source share
2 answers

1) The Objective-c indentation convention is aligned with a colon when multiple parameters are specified.

Example

function:

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

Must be formatted:

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

I came with Java, and it was very difficult for me to get used to them at first. But, having tried it several times, this is really the most pleasant way.

2) I do not think (I'm not sure) that you can change this in xcode. Apple's agreement is pretty clear like this.

EDIT: Challenges. I personally start with the first parameter and align the rest:

 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]; 
+5
source

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

  • Use Uncristify , there is a GUI tool to configure it: UncrustifyX
  • Use the AppCode, Jetbrains iOS / OSX IDE - it is customizable and built-in formatting. See screenshot.

Appcode code formatting tool

+3
source

Source: https://habr.com/ru/post/902281/


All Articles