Objective-C tab settings and whitespace style

亡梦爱人 提交于 2019-12-04 17:15:07

问题


Apple's Objective-C documentation, references, and generated code seem to be totally inconsistent with their coding style. I can not determine the "preferred" style (if one exists) for Objective-C and Cocoa source code.

Here's what I've ran into so far.

Tab settings

Xcode's default setting is set to tabs. However, generated projects are always created with 4 spaces regardless of your tab settings. Worse, some generated code has stray tabs hanging around. Downloaded code examples seem to vary from project to project as well.

Method definition spacing

In Apple's example code, different styles of method definition even vary within a single file. But the most common style of spacing seems to include a space between the - or + and spaces only between addition arguments. This style is also used in the Xcode reference library which leads me to believe it is the preferred style.

- (IBAction)reloadData:(id)sender;
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

Inconsistent variates:

- (IBAction) reloadData:(id)sender;
- (IBAction)reloadData:(id) sender;
-(IBAction)reloadData:(id)sender;
- (IBAction) reloadData: (id)sender;
- (id)tableView: (NSTableView *)aTableView objectValueForTableColumn: (NSTableColumn *)aTableColumn row: (NSInteger)rowIndex

Brace style

Another thing that is widely inconsistent is the brace style. There are two popular styles:

Open brace on its own line:

  @interface Foo : NSObject
  {
  }

  - (id)init
  {
  }

Open brace on the same line as the method declaration:

  @interface Foo : NSObject {
  }

  - (id)init {
  }

Question

Is there a recommended coding style for Objective-C by Apple? If not, what style do you prefer and why?


回答1:


I found that I was constantly setting the generated projects to my own preferred style, so I edited the template files to my preferences (tabs for indent, braces on a line of their own). The files are here:

/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/

I select everything, get rid of all indentation with cmd-[ (as many times as the document needs) and do Edit->Format->re-indent. I have this hooked up to shift-cmd-[ for convenience.

I've seen the differences in style for example code and project templates and just assumed it was due to the age of the files being different. I guess they have had different internal coding style guides which have changed.

Here's the Google Obj-C style guide:

http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml

and another nice one from CocoaDevCentral:

http://www.cocoadevcentral.com/articles/000082.php

of if you really want to follow Apple rules, the Objective C programming guide has a bunch of code examples to take rules from.

https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/



来源:https://stackoverflow.com/questions/1077792/objective-c-tab-settings-and-whitespace-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!