Objective-C选项卡设置和空格样式 [英] Objective-C tab settings and whitespace style

查看:182
本文介绍了Objective-C选项卡设置和空格样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple的Objective-C文档,引用和生成的代码似乎与他们的编码风格完全不一致。我不能为Objective-C和Cocoa源代码确定首选样式(如果存在)。



这是我到目前为止所遇到的。 p>

标签设置



Xcode的默认设置设置为标签。但是,无论您的选项卡设置如何,生成的项目始终创建4个空格。更糟的是,一些生成的代码有悬挂的标签。



方法定义间距



在Apple的示例代码中,不同风格的方法定义甚至在单个文件内变化。但是最常见的间距样式似乎包括 - + 之间的空格和仅在加法参数之间的空格。这种风格也用于Xcode参考库,这使我相信它是首选风格。

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

不一致的变量:

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



大括号风格



另一件广泛不一致的东西是大括号风格。有两种受欢迎的样式:



在自己的行上打开括号:

  @interface Foo:NSObject 
{
}

- (id)init
{
}

在方法声明的同一行中打开大括号:

  @interface Foo:NSObject {
}

- (id)init {
}



问题



Apple提供了Objective-C的建议编码风格?

解决方案

我发现我不断地将生成的项目设置为我自己喜欢的风格,所以我编辑模板文件到我的首选项(制表符缩进,大括号在自己的一行)。文件位于:



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



我选择所有内容,使用cmd- [(与文档需要的次数相同)删除所有缩进,然后执行Edit-> Format-> re-indent。我已经连接到了shift-cmd- [为了方便起见。



我已经看到了示例代码和项目模板的风格的差异,文件的时代不同。我猜他们有不同的内部编码风格指南已经改变。



以下是Google Obj-C风格指南:



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



和另一个来自CocoaDevCentral的好方法:



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



如果您真的想关注苹果规则,Objective C编程指南有一堆代码示例来从中获取规则。



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


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?

解决方案

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/

这篇关于Objective-C选项卡设置和空格样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆