googleOjbectc规范_计算机软件及应用_IT计算机_专业资料.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《googleOjbectc规范_计算机软件及应用_IT计算机_专业资料.docx》由会员分享,可在线阅读,更多相关《googleOjbectc规范_计算机软件及应用_IT计算机_专业资料.docx(23页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Gooqle 的 Obiective-C 编码规范文章分类:移动开发原文 Gooale Obiective-C Stvle GuideiPhone项冃新成立,也没有编码规范的积累,项冃组本米是想拿老的C编码规范套用的,但评审 就发现问题多多,之后找到了 Google的Objective-C的编码规范,大家就先翻译一下咯声明这是无版权翻译,也不对任何错误负责,不保证文章的完整性,我到现在也认不全语法。总览背景知识Objective-C是个C语言的扩展语言,非常动态,非常的“面向对象”,它被设计成既拥有复杂的面 向对象设计理念又可以轻松使用与阅读的语言,也是MacS X和iPhone开发的首选语言
2、。Cocoa是Mac OS X的主要应用框架,提供迅速开发各种功能的Mac OS X应用的Objective-C类集 合。Apple已经有一个很好也被广泛接受的Objective-C的编码规范,Google也有类似的C+编码规范, 这份。bjective-C编码规范很自然是Apple和Google的共同推荐的组合。所以,在阅读本规范前,确保你 已经阅读了:Apples Cocoa Codina GuidelinesGoogles Open Source C+ St+le Guide注意所有已在Google的C+编码规范里的禁用条款在。bjective-C里也适用,除作本文档明确指出 反对意见。
3、本文档旨在描述可供可适用于所有Mac OS X代码的Objective-C(包括Objective-C+)编码规范和实 践。规范中的许多条款已经改进也不断的被其他的项目和团队所证明其指导性。Google的相关开源项目都 遵守此规范。Google已经发布了 份作为Gooqle Toolbox for Mac project (文档中简称为GTM)的组成部分的遵 守本规范的开源代码。这份开放代码也是本文很好的例证(原文看不太懂Code meant to be shared across different projects is a good candidate to be included in
4、 this repository.)注意本文不是bjective-C的教学指南,我们假设读者已经了解语言。如果你是个。bjective-C的 初学者或需要重温,请阅读The。60近CProgramming Language .人们说个例子胜过千言万语,所以就让我们用例子来让你感受以下编码规范的风格,留间距,命名下例是份头文件,展示对访1。什20声明正确的注释和留间距Java代码1. /GTMFoo.h2. /FooProject3. /4. /Created byGreg Milleron6/13/08.5. /Copyright 2008 Google,Inc.Allrights reser
5、ved.6. /7.8. #import 9.10. / A sample class demonstrating good Objective-C style. All interfaces,11. / categories, and protocols (read: all top-level declarations in a heade r)12. / MUST be commented. Comments must also be adjacent to the object they * re13. / documenting.14. /15. / (no blank line b
6、etween this comment and the interface)16. interface GTMFoo : NSObject 17. private18. NSString *foo_;19. NSString *bar_;20. 21.22. / Returns an autoreleased instance of GMFoo. See -initWithString: for detai Is23. / about the argument.24. + (id)fooWithString:(NSString *)string;25.26. / Designated init
7、ializer. |string| will be copied and assigned to |foo_|.27. - (id)initWithString:(NSString *)string;28.29. / Gets and sets the string for |foo.30. - (NSString *)foo;31. - (void)setFoo:(NSString *)newFoo;32.33. / Does some work on |blah| and returns YES if the work was completed34. / successfuly, and
8、 NO otherwise.35. - (BOOL)doWorkWithString:(NSString *)blah;36.37. end下例是一份源文件,展示对接口的implementation的实现的正确注释和留间隔。它也包括了主要方 法如 getters,setters,init,和 dealloc 的相关实现。Java代码1. /2. /GTMFoo.m3. /FooProject4. /5. /Created byGreg Milleron6/13/08.6. /Copyright 2008 Google,Inc.Allrights reserved.7. /8.9. #impor
9、t GTMFoo.h10.11.12. 0implementation GTMFoo13.14. + (id)fooWithString:(NSString *)string 15. return self alloc initWithString:string autorelease;16. 17.18. / Must always override supers designated initializer.19. - (id)init 20. return self initWithString:nil;21. 22.23. - (id)initWithString:(NSString
10、*)string 24. if (self = super init) 25. foo_ = string copy;26. bar_ = NSString alloc initWithFormat:hi %d, 3;27. 28. return self;29. 30.31. - (void)dealloc 32. foo_ release;33. bar_ release;34. super dealloc;35. 36.37. - (NSString *)foo 38. return foo_;39. 40.41. - (void)setFoo:(NSString *)newFoo 42
11、. foo_ autorelease;43. foo_ = newFoo copy;44. 45.46. - (BOOL)doWorkWithString:(NSString *)blah 4748. return NO;49. 50.51. end间隔与格式化空格对tab键仅使用空格,缩进两个。我们使用空格用于缩进,不要在编码时使用tab键,你应该设置你的编辑器将tab键转换成对应的空 格。行长度代码中的每行文本不要超过80个字符的长度。尽管。bjective-C正变得比C+更加繁冗,为了保持规范的互通性,我们还是决定保持80字符长度 的限制。这比你想象中的更容易做到。我们知道本条款是有争议
12、的,但已有此多的代码己经遵从了本条款,即使只是保持致性也是个充 足的理由。你可以在Xcode里清楚地发现代码中的违规,设置Xcode Preferences Text Editing Show page guide.(之后就可以在代码编辑区域里看到条指定字符长度的指示线了)方法声明与定义留一个空格在或+和返冋类型之间,但参数列表里的参数之间不要留间隔。方法应该写成这样:Java代码1. - (void)doSomethingWithString:(NSString *)theString 3星号前的空格是可选的,你可以根据原来的代码风格门行决定。如果参数过多,推荐每个参数各占一行。使用多行的情
13、况下,以参数前的冒号用于对齐:(很遗憾这里仅有Google Chrome浏览器能看出是冒号对齐的)Java代码2. - (void)doSomethingWith:(GTMFoo *)theFoo3. rect:(NSRect)theRect4. interval:(float)thelnterval 5. .6. 当第一个关键字比其他的短时,后续行至少缩进四个空格。这样你可以让后续的关键字垂直对齐,而 不是用冒号对齐:Java代码1. - (void)short:(GTMFoo *)theFoo2. longKeyword:(NSRect)theRect3. evenLongerKeywor
14、d:(float)thelnterval 4. .5. 方法调用方法调用的格式和方法声明时的格式时一致的,如果格式风格可选,遵从原有代码的风格。调用应该将所有参数写在一行:Java代码1. myObject doFooWith:argl name:arg2 error:arg3;或者每个参数一行,用冒号对齐:(对齐效果如前说明)Java代码2. myObject doFooWith:argl3. name:arg24. error:arg3;不要使用如下风格的写法Java代码123456789myObject doFooWith:argl name:arg2 / some lines with
15、 1 arg error:arg3;myObject doFooWith:arglname:arg2 error:arg3;myObject doFooWith:arglname:arg2 / aligning keywords instead of colons error:arg3;在声明和定义时,如果因为关键字长度使就算有四个空格在前仍然无法用冒号对齐,那么就让后续行 缩进四个空格而不是冒号对齐:Java代码1. myObj short:argl2. longKeyword:arg23. evenLongerKeyword:arg3;public 和 private权限控制符public
16、和private缩进个空格.类似 C+的 public,protected,private:Java代码1. ginterface MyClass : NSObject 2. public3. 4. 0private5. 6. 7. end异常每个异常标签的和开括号(写在统行,标签和开括号间隔个空格,同样适用于catch语句。如果你决定使用bjective-C的异常,那么就按如下格式,不过你最好先看看Avoid Throwing Exceptions(见后)条款,了解为何你不应使用异常。Java代码1. try 2. foo();3. 4. catch (NSException *ex) 5.
17、 bar(ex);6. 7. 0finally 8. baz();9. 命名命名规则对于维护代码来说是非常重要的。Objective-C方法名往往很长,不过这也有好处,读代码 就像读散文(放屁),让很多注释变得毫无意义。写纯Objective-C代码时,我们基本上遵守标准Obiective-C namina rules ,这些规则和C+的规则 有很大的不同。比如Google的C+代码规范推荐变量名构词之间使用下划线隔开,而本文档推荐驼峰法, 也是bjective-C社区的标准。所有类,类别,方法,以及变量如包括缩写,则缩写部分使用全大的缩写(Initialisms)形式。这遵守 Apple的标
18、准,比如URL,TIFF以及EXIF。当写Objective-C+代码时,情况就不是那么单一了。许多项冃需要实现带些Objective-C代码的跨 平台的C+APIS或者连接后台的C+代码与前台的原生Cocoa代码.这会造成两种规范直接冲突。我们的解决方法是根据方法/函数风格来决定。如果在Qimplementation块,就使用Objective-C的命 名规则:如果在C+的方法实现块,就使用C+的命名规则。避免了实体变量和本地变量在一个函数内命 名规则冲突的情况,而这种情况是对可读性的极大损害。文件命名文件名反映了它所包含的实现类的名字,遵从你所在项冃的习惯。文件扩展名使用如下规则.h C/
19、C+/Objective-C header file .m Objective-C implementation file .mm Objective-C+ implementation file .cc Pure C+ implementation file.cC implementation file类别的文件名应该包含扩展类的名字,比如GTMNSString+Utils.h or GTMNSTextView+Autocomplete.hObjective-C+在份源文件里,Objective-C+代码遵守当前方法/函数的风格为了尽量减少不同命名风格间的冲突,使用当前方法的风格。如果在im
20、plementation块,使用 Objective-C命名规则,如果在C+类的函数实现块,使用C+命名规则。Java代码1. / file: cross_platform_header.h2.3. class CrossPlatformAPI 4. public:5. .6. int DoSomethingPlatformSpecific(); / impl on each platform7. private:8. int an_instance_var_;9;10.11. / file: mac_implementation.mm12. #include Mcross_platform_
21、header.h13.14. / A typical Objective-C class, using Objective-C naming.15. interface MyDelegate : NSObject 16. gprivate17. int instanceVar_;18. CrossPlatformAPI* backEndObject_;19. 20. - (void)respondToSomething:(id)something;21. end22. 0implementation MyDelegate23. - (void)respondToSomething:(id)so
22、mething 24. / bridge from Cocoa through our C+ backend25. instanceVar_ = backEndObject-DoSomethingPlatformSpecific();26. NSString* tempString = NSString stringWithlnt:instanceVar_;27. NSLog(%, tempString);28. 29. end30.31. / The platform-specific implementation of the C+ class, using32. / C+ naming.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- googleOjbectc 规范 计算机软件 应用 _IT 计算机 专业 资料
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内