没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Google Objective-C Style Guide
Revision 2.36
Mike Pinkerton
Greg Miller
Dave MacLachlan
Each style point has a summary for which additional information is available by toggling the
accompanying arrow button that looks this way: ▽ . You may toggle all summaries with the big arrow
button:
▽ Toggle all summaries
Table of Contents
Example
Spacing And
Formatting
Spaces vs. Tabs Line Length Method Declarations and Definitions Method
Invocations @public and @private Exceptions Protocols Blocks
Naming
File Names Objective-C++ Class Names Category Names Objective-C Method
Names Variable Names
Comments
File Comments Declaration Comments Implementation Comments Object
Ownership
Cocoa and
Objective-C
Features
Member Variables Should Be @private Identify Designated Initializer Override
Designated Initializer Overridden NSObject Method Placement Initialization Avoid
+newKeep the Public API Simple #import and #include Use Root
Frameworks Prefer To autorelease At Time of Creation Autorelease Then
Retain Avoid Accessors During init and deallocDealloc Instance Variables in
Declaration Order Setters copy NSStrings Avoid Throwing Exceptions nil
Checks BOOL Pitfalls Properties Interfaces Without Instance
VariablesAutomatically Synthesized Instance Variables
Cocoa
Patterns
Delegate Pattern Model/View/Controller
Important Note
Displaying Hidden Details in this Guide
link ▽
This style guide contains many details that are initially hidden from view. They are marked by
the triangle icon, which you see here on your left. Click it now. You should see "Hooray"
appear below.
Hooray! Now you know you can expand points to get more details. Alternatively, there's an
"expand all" at the top of this document.
Background
Objective-C is a very dynamic, object-oriented extension of C. It's designed to be easy to use
and read, while enabling sophisticated object-oriented design. It is the primary development
language for new applications on Mac OS X and the iPhone.
Cocoa is one of the main application frameworks on Mac OS X. It is a collection of Objective-
C classes that provide for rapid development of full-featured Mac OS X applications.
Apple has already written a very good, and widely accepted, coding guide for Objective-C.
Google has also written a similar guide for C++. This Objective-C guide aims to be a very
natural combination of Apple's and Google's general recommendations. So, before reading
this guide, please make sure you've read:
• Apple's Cocoa Coding Guidelines
• Google's Open Source C++ Style Guide
Note that all things that are banned in Google's C++ guide are also banned in Objective-C++,
unless explicitly noted in this document.
The purpose of this document is to describe the Objective-C (and Objective-C++) coding
guidelines and practices that should be used for all Mac OS X code. Many of these guidelines
have evolved and been proven over time on other projects and teams. Open-source projects
developed by Google conform to the requirements in this guide.
Google has already released open-source code that conforms to these guidelines as part of
the Google Toolbox for Mac project (abbreviated GTM throughout this document). Code
meant to be shared across different projects is a good candidate to be included in this
repository.
Note that this guide is not an Objective-C tutorial. We assume that the reader is familiar with
the language. If you are new to Objective-C or need a refresher, please read The Objective-C
Programming Language .
Example
They say an example is worth a thousand words so let's start off with an example that should
give you a feel for the style, spacing, naming, etc.
An example header file, demonstrating the correct commenting and spacing for
an @interface declaration
// Foo.h // AwesomeProject // // Created by Greg Miller on
6/13/08. // Copyright 2008 Google, Inc. All rights reserved. //
#import <Foundation/Foundation.h> // A sample class demonstrating
good Objective-C style. All interfaces, // categories, and protocols
(read: all top-level declarations in a header) // MUST be commented.
Comments must also be adjacent to the object they're // documenting.
// // (no blank line between this comment and the interface)
@interface Foo : NSObject { @private NSString *bar_; NSString
*bam_; } // Returns an autoreleased instance of Foo. See -
initWithBar: for details // about |bar|. + (id)fooWithBar:(NSString
*)bar; // Designated initializer. |bar| is a thing that represents a
thing that // does a thing. - (id)initWithBar:(NSString *)bar; //
Gets and sets |bar_|. - (NSString *)bar; - (void)setBar:(NSString
*)bar; // Does some work with |blah| and returns YES if the work was
completed // successfully, and NO otherwise. -
(BOOL)doWorkWithBlah:(NSString *)blah; @end
An example source file, demonstrating the correct commenting and spacing for
the @implementation of an interface. It also includes the reference implementations for
important methods like getters and setters, init, and dealloc.
// // Foo.m // AwesomeProject // // Created by Greg Miller on
6/13/08. // Copyright 2008 Google, Inc. All rights reserved. //
#import "Foo.h" @implementation Foo + (id)fooWithBar:(NSString
*)bar { return [[[self alloc] initWithBar:bar] autorelease]; } //
Must always override super's designated initializer. - (id)init
{ return [self initWithBar:nil]; } - (id)initWithBar:(NSString
*)bar { if ((self = [super init])) { bar_ = [bar copy];
bam_ = [[NSString alloc] initWithFormat:@"hi %d", 3]; } return
self; } - (void)dealloc { [bar_ release]; [bam_ release];
[super dealloc]; } - (NSString *)bar { return bar_; } -
(void)setBar:(NSString *)bar { [bar_ autorelease]; bar_ = [bar
copy]; } - (BOOL)doWorkWithBlah:(NSString *)blah { // ... return
NO; } @end
Blank lines before and after @interface, @implementation, and @end are optional. If
your @interface declares instance variables, a blank line should come after the closing
brace (}).
Unless an interface or implementation is very short, such as when declaring a handful of
private methods or a bridge class, adding blank lines usually helps readability.
Spacing And Formatting
Spaces vs. Tabs
link ▽
Use only spaces, and indent 2 spaces at a time.
We use spaces for indentation. Do not use tabs in your code. You should set your editor to
emit spaces when you hit the tab key.
Line Length
link ▽
Each line of text in your code should try to be at most 80 characters long.
Strive to keep your code within 80 columns. We realize that Objective C is a verbose
language and in some cases it may be more readable to extend slightly beyond 80 columns,
but this should definitely be the exception and not commonplace.
If a reviewer asks that you reformat a line because they feel it can be fit in 80 columns and
still be readable, you should do so.
We recognize that this rule is controversial, but so much existing code already adheres to it,
and we feel that consistency is important.
You can make violations easier to spot in Xcode by going to Xcode > Preferences > Text
Editing > Show page guide.
Method Declarations and Definitions
link ▽
One space should be used between the - or + and the return type, and no spacing in the
parameter list except between parameters.
Methods should look like this:
- (void)doSomethingWithString:(NSString *)theString { ... }
The spacing before the asterisk is optional. When adding new code, be consistent with the
surrounding file's style.
If you have too many parameters to fit on one line, giving each its own line is preferred. If
multiple lines are used, align each using the colon before the parameter.
- (void)doSomethingWith:(GTMFoo *)theFoo
rect:(NSRect)theRect interval:(float)theInterval
{ ... }
When the first keyword is shorter than the others, indent the later lines by at least four spaces.
You can do this by making keywords line up vertically, not aligning colons:
- (void)short:(GTMFoo *)theFoo longKeyword:(NSRect)theRect
evenLongerKeyword:(float)theInterval { ... }
Method Invocations
link ▽
Method invocations should be formatted much like method declarations. When there's a
choice of formatting styles, follow the convention already used in a given source file.
Invocations should have all arguments on one line:
[myObject doFooWith:arg1 name:arg2 error:arg3];
or have one argument per line, with colons aligned:
[myObject doFooWith:arg1 name:arg2
error:arg3];
Don't use any of these styles:
[myObject doFooWith:arg1 name:arg2 // some lines with >1 arg
error:arg3]; [myObject doFooWith:arg1 name:arg2
error:arg3]; [myObject doFooWith:arg1 name:arg2 //
aligning keywords instead of colons error:arg3];
剩余19页未读,继续阅读
资源评论
- holdu2012-09-28内容不是很多
animeng
- 粉丝: 196
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功