Objective c学习笔记Word文档格式.docx
《Objective c学习笔记Word文档格式.docx》由会员分享,可在线阅读,更多相关《Objective c学习笔记Word文档格式.docx(41页珍藏版)》请在冰豆网上搜索。
所以,在阅读本规范前,确保你已经阅读了:
Apple'
sCocoaCodingGuidelines
Google'
sOpenSourceC++StyleGuide
注意所有已在Google的C++编码规范里的禁用条款在Objective-C里也适用,除非本文档明确指出反对意见。
本文档旨在描述可供可适用于所有MacOSX代码的Objective-C(包括Objective-C++)编码规范和实践。
规范中的许多条款已经改进也不断的被其他的项目和团队所证明其指导性。
Google的相关开源项目都遵守此规范。
Google已经发布了一份作为GoogleToolboxforMacproject
(文档中简称为GTM)的组成部分的遵守本规范的开源代码。
这份开放代码也是本文很好的例证(原文看不太懂--Codemeanttobesharedacrossdifferentprojectsisagoodcandidatetobeincludedinthisrepository.)
注意本文不是Objective-C的教学指南,我们假设读者已经了解语言。
如果你是一个Objective-C的初学者或需要重温,请阅读TheObjective-CProgrammingLanguage
.
示例
人们说一个例子胜过千言万语,所以就让我们用例子来让你感受以下编码规范的风格,留间距,命名等等。
下例是一份头文件,展示对@interface
声明正确的注释和留间距
Java代码
1.//
GTMFoo.h
2.//
FooProject
3.//
4.//
Created
by
Greg
Miller
on
6/13/08.
5.//
Copyright
2008
Google,
Inc.
All
rights
reserved.
6.//
7.
8.#import
<
Foundation/Foundation.h>
9.
10.//
A
sample
class
demonstrating
good
Objective-C
style.
interfaces,
11.//
categories,
and
protocols
(read:
all
top-level
declarations
in
a
header)
12.//
MUST
be
commented.
Comments
must
also
adjacent
to
the
object
they'
re
13.//
documenting.
14.//
15.//
(no
blank
line
between
this
comment
interface)
16.@interface
GTMFoo
:
NSObject
{
17.
@private
18.
NSString
*foo_;
19.
*bar_;
20.}
21.
22.//
Returns
an
autoreleased
instance
of
GMFoo.
See
-initWithString:
for
details
23.//
about
argument.
24.+
(id)fooWithString:
(NSString
*)string;
25.
26.//
Designated
initializer.
|string|
will
copied
assigned
|foo_|.
27.-
(id)initWithString:
28.
29.//
Gets
sets
string
30.-
*)foo;
31.-
(void)setFoo:
*)newFoo;
32.
33.//
Does
some
work
|blah|
returns
YES
if
was
completed
34.//
successfuly,
NO
otherwise.
35.-
(BOOL)doWorkWithString:
*)blah;
36.
37.@end
viewplainprint?
{
20.}
下例是一份源文件,展示对接口的@implementation
的实现的正确注释和留间隔。
它也包括了主要方法如getters,setters,init
和dealloc
的相关实现。
GTMFoo.m
7.//
8.
9.#import
"
GTMFoo.h"
10.
11.
12.@implementation
GTMFoo
13.
14.+
*)string
15.
return
[[[self
alloc]
initWithString:
string]
autorelease];
16.}
18.//
Must
always
override
super'
s
designated
19.-
(id)init
20.
[self
nil];
21.}
22.
23.-
24.
((self
=
[super
init]))
foo_
[string
copy];
26.
bar_
[[NSString
initWithFormat:
@"
hi
%d"
3];
27.
}
self;
29.}
30.
(void)dealloc
[foo_
release];
33.
[bar_
34.
dealloc];
35.}
37.-
*)foo
38.
foo_;
39.}
40.
41.-
*)newFoo
42.
43.
[newFoo
44.}
45.
46.-
*)blah
47.
//
...
48.
NO;
49.}
50.
51.@end
16.}
21.}
}
29.}
35.}
39.}
44.}
49.}
间隔与格式化
空格对tab键
仅使用空格,缩进两个。
我们使用空格用于缩进,不要在编码时使用tab键,你应该设置你的编辑器将tab键转换成对应的空格。
行长度
代码中的每行文本不要超过80个字符的长度。
尽管Objective-C正变得比C++更加繁冗,为了保持规范的互通性,我们还是决定保持80字符长度的限制。
这比你想象中的更容易做到。
我们知道本条款是有争议的,但已有此多的代码已经遵从了本条款,即使只是保持一致性也是一个充足的理由。
你可以在Xcode里清楚地发现代码中的违规,设置
Xcode>
Preferences>
TextEditing>
Showpageguide.
(之后就可以在代码编辑区域里看到一条指定字符长度的指示线了)
方法声明与定义
留一个空格在-或+和返回类型之间,但参数列表里的参数之间不要留间隔。
方法应该写成这样:
1.-
(void)doSomethingWithString:
*)theString
2.
...
3.}
星号前的空格是可选的,你可以根据原来的代码风格自行决定。
如果参数过多,推荐每个参数各占一行。
使用多行的情况下,以参数前的冒号用于对齐:
(很遗憾这里仅有GoogleChrome浏览器能看出是冒号对齐的......)
(void)doSomethingWith:
(GTMFoo
*)theFoo
rect:
(NSRect)theRect
3.
interval:
(float)theInterval
4.
5.}
*)theFoo
(NSRect)theRect
当第一个关键字比其他的短时,后续行至少缩进四个空格。
这样你可以让后续的关键字垂直对齐,而不是用冒号对齐:
(void)short:
longKeyword:
evenLongerKeyword:
方法调用
方法调用的格式和方法声明时的格式时一致的,如果格式风格可选,遵从原有代码的风格。
调用应该将所有参数写在一行:
1.[myObject
doFooWith:
arg1
name:
arg2
error:
arg3];
或者每个参数一行,用冒号对齐:
(对齐效果如前说明)
arg1
arg2
不要使用如下风格的写法
lines
with
>
1
arg
4.[myObject
5.
6.
7.[myObject
aligning
keywords
instead
colons
line