你如何调整Xcode的Objective C参数的自动缩进?

时间:2023-01-14 16:05:38

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

这是我的iOS应用程序的一小段代码:

    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:

为什么Xcode缩进线如此血腥?作为一个旧的Perl,Ruby和JavaScript猴子,我更倾向于手动缩进它,如下所示:

    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.

所以参数名称和值都是左对齐的,并且只缩进一个级别(对我来说是4个空格)。这使用了更少的屏幕空间,而且我不太可能在MacBook Air上处理包装线(这使得最右边的缩进内容甚至更难阅读)。

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

但是,我认为Apple有一些理由更喜欢第一种方法,因此有这样的Xcode缩进。或者有吗?

So I have two question:

所以我有两个问题:

  • How do you prefer to indent method parameters in your Objective C code?
  • 您更喜欢在Objective C代码中缩进方法参数?
  • How do you tweak Xcode to format with your preferred indentation style?
  • 你如何调整Xcode以使用您喜欢的缩进样式进行格式化?

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.

不试图在这里开始圣战;我更喜欢Objective-C程序员中最好的做法,Xcode如何帮助实现这些做法,并找出Xcode是否有充分理由采用默认方式。


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?

这似乎是比Google风格指南的线长建议更好的默认值,不是吗?

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

有没有办法调整Xcode以这种方式格式化参数?

2 个解决方案

#1


5  

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

1)当给出多个参数时,Objective-c缩进约定在冒号处对齐。

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.

我来自Java,起初我很难习惯。但尝试不同几次后,这真的是最好的方式。

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.

2)我不认为(我不确定)你可以在xcode中改变它。 Apple的惯例非常清楚它是如何实现的。

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

#2


3  

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

我不确定如何在Xcode中格式化它,但代码格式化工具的两个选项是:

  1. Use Uncristify, there's a GUI tool to configure it: UncrustifyX
  2. 使用Uncristify,有一个GUI工具来配置它:UncrustifyX
  3. Use AppCode, the iOS/OSX IDE from Jetbrains - it has configurable and built in formatting. See screenshot.
  4. 使用AppCode,Jetbrains的iOS / OSX IDE - 它具有可配置和内置格式。见截图。

你如何调整Xcode的Objective C参数的自动缩进?

#1


5  

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

1)当给出多个参数时,Objective-c缩进约定在冒号处对齐。

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.

我来自Java,起初我很难习惯。但尝试不同几次后,这真的是最好的方式。

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.

2)我不认为(我不确定)你可以在xcode中改变它。 Apple的惯例非常清楚它是如何实现的。

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

#2


3  

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

我不确定如何在Xcode中格式化它,但代码格式化工具的两个选项是:

  1. Use Uncristify, there's a GUI tool to configure it: UncrustifyX
  2. 使用Uncristify,有一个GUI工具来配置它:UncrustifyX
  3. Use AppCode, the iOS/OSX IDE from Jetbrains - it has configurable and built in formatting. See screenshot.
  4. 使用AppCode,Jetbrains的iOS / OSX IDE - 它具有可配置和内置格式。见截图。

你如何调整Xcode的Objective C参数的自动缩进?