7

这是我的 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
                          ];

为什么 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
    ];

所以参数名称和值都是左对齐的,并且只缩进一级(我是 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
];

这似乎比谷歌风格指南的行长推荐更好,不是吗?

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

4

2 回答 2

5

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];
于 2011-11-24T07:31:46.930 回答
3

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

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

AppCode 代码格式化工具

于 2013-01-14T08:04:30.543 回答