Objective-C tab settings and whitespace style

2019-03-25 01:56发布

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条回答
家丑人穷心不美
2楼-- · 2019-03-25 02:36

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/

查看更多
登录 后发表回答