gpt4 book ai didi

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

转载 作者:bug小助手 更新时间:2023-10-28 10:52:07 26 4
gpt4 key购买 nike

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

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

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

这似乎是比 the Google style guide's line length recommendation 更好的默认值,不是吗?

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

最佳答案

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 中更改它。 Apple 的约定非常清楚。

编辑:调用。我个人从第一个参数开始,然后将其余参数对齐:

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

关于objective-c - 你如何调整 Xcode 的 Objective C 参数的自动缩进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8253469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com