这是我的 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 以这种方式格式化参数?