下面是一個代碼片段從我的iOS應用:如何調整Xcode的Objective C參數自動縮進?
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上處理包裝線(這使得極右縮進的東西更難讀)。
但是,我認爲蘋果傾向於使用第一種方法是有原因的,因此Xcode會以這種方式縮進。或者在那裏?
所以,我有兩個問題:
- 你怎麼喜歡縮進方法的參數在目標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格式化參數?
「你怎麼喜歡縮進......」 是[不是一個好的計算器問題(http://stackoverflow.com/faq#dontask)。至於「你如何調整Xcode ...」,沒有任何選項可以使它自動縮進多行選擇器。 –
FWIW,您可以通過在init之前添加換行符來獲得與您想要的類似的行爲。它自動縮進排列冒號。 – jrturton
@robmayoff:好的,謝謝。我已經更新了這個問題。 – theory