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

发布于 2024-12-17 16:24:58 字数 2473 浏览 0 评论 0原文

以下是我的 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 rows so bad?作为一个老 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
    ];

因此参数名称和值都是左对齐的,并且仅缩进一级(对我来说是 4 个空格)。这使用的屏幕空间要少得多,而且我不太可能需要在 MacBook Air 上处理换行符(这使得最右侧的缩进内容更难以阅读)。

然而,我认为 Apple 更喜欢第一种方法是有原因的,因此 Xcode 会以这种方式缩进。或者有吗?

所以我有两个问题:

  • 你喜欢如何在 Objective C 代码中缩进方法参数?
  • 如何调整 Xcode 以使用您喜欢的缩进样式进行格式化?

并不是想在这里发动一场圣战;我更好奇什么是 Objective-C 程序员的最佳实践,Xcode 如何帮助实现这些实践,并找出 Xcode 的默认方式是否有充分的理由。


更新:评论者和答案指出,默认格式将参数的冒号对齐。我应该在发布之前记住这一点,因为我当然注意到了它,并且当最长的项目没有缩进太多时,它看起来会很漂亮。但我发现,通常情况下,最长的项目会缩进很多。例如:

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

这是为什么?即使我希望它们在冒号处对齐(​​并且我经常手动格式化它们来这样做),缩进这么多似乎很愚蠢。为什么它坚持缩进到开头冒号的水平?为什么不只是一层缩进,使右括号向外凹陷以显示缩进“块”的末尾?例如:

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

这似乎是比Google 样式指南的行长度建议更好的默认值< /a>,不是吗?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪若未夕 2024-12-24 16:24:58

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

示例

函数:

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

必须格式化为:

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

我来自 Java,一开始对我来说真的很难习惯。但在尝试了几次之后,这确实是最好的方法。

2)我不认为(我不确定)你可以在xcode中更改它。苹果公司的惯例非常清楚。

编辑:
祈求。我个人从第一个参数开始,然后将其余参数对齐,如下所示:

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

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];
给妤﹃绝世温柔 2024-12-24 16:24:58

我不确定如何在 Xcode 中对其进行格式化,但是代码格式化工具有两个选项:

  1. 使用 Uncristify ,有一个 GUI 工具来配置它: UncrustifyX
  2. 使用 AppCode,来自 Jetbrains 的 iOS/OSX IDE - 它具有可配置和内置格式。请参阅屏幕截图。

AppCode 代码格式化工具

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文