<table class="docutils field-list" frame="void" rules="none"><col class="field-name"/><col class="field-body"/><tbody valign="top"><tr class="field-odd field"><th class="field-name">版本:</th><td class="field-body"><p class="first">2.36</p></td></tr><tr class="field-even field"><th class="field-name">原作者:</th><td class="field-body"><div class="first line-block"><div class="line">Mike Pinkerton</div><div class="line">Greg Miller</div><div class="line">Dave MacLachlan</div></div></td></tr><tr class="field-odd field"><th class="field-name">翻譯:</th><td class="field-body"><div class="first line-block"><div class="line"><a class="reference external" href="http://ke.indiebros.com/">ewangke</a><span class="link-target"> [http://ke.indiebros.com/]</span></div><div class="line"><a class="reference external" href="http://yangyubo.com">brantyoung</a><span class="link-target"> [http://yangyubo.com]</span></div></div></td></tr><tr class="field-even field"><th class="field-name">項目主頁:</th><td class="field-body"><ul class="first last simple"><li><a class="reference external" href="http://google-styleguide.googlecode.com">Google Style Guide</a><span class="link-target"> [http://google-styleguide.googlecode.com]</span></li><li><a class="reference external" href="http://github.com/zh-google-styleguide/zh-google-styleguide">Google 開源項目風格指南 - 中文版</a><span class="link-target"> [http://github.com/zh-google-styleguide/zh-google-styleguide]</span></li></ul></td></tr></tbody></table>
## 譯者的話
### ewanke
一直想翻譯這個 [style guide](http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml) [http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml] ,終于在周末花了7個小時的時間用vim敲出了HTML。很多術語的翻譯很難,平時看的中文技術類書籍有限,對很多術語的中文譯法不是很清楚,難免有不恰當之處,請讀者指出并幫我改進:王軻 ”ewangke at gmail.com” 2011.03.27
### brantyoung
對 Objective-C 的了解有限,憑著感覺和 C/C++ 方面的理解:
- 把指南更新到 2.36 版本
- 調整了一些術語和句子
## 背景介紹
Objective-C 是 C 語言的擴展,增加了動態類型和面對對象的特性。它被設計成具有易讀易用的,支持復雜的面向對象設計的編程語言。它是 Mac OS X 以及 iPhone 的主要開發語言。
Cocoa 是 Mac OS X 上主要的應用程序框架之一。它由一組 Objective-C 類組成,為快速開發出功能齊全的 Mac OS X 應用程序提供支持。
蘋果公司已經有一份非常全面的 Objective-C 編碼指南。Google 為 C++ 也寫了一份類似的編碼指南。而這份 Objective-C 指南則是蘋果和 Google 常規建議的最佳結合。因此,在閱讀本指南之前,請確定你已經閱讀過:
- [Apple’s Cocoa Coding Guidelines](http://developer.apple.com/documentation/Cocoa/Conceptual/CodingGuidelines/index.html) [http://developer.apple.com/documentation/Cocoa/Conceptual/CodingGuidelines/index.html]
- [Google’s Open Source C++ Style Guide](http://codinn.com/projects/google-cpp-styleguide/) [http://codinn.com/projects/google-cpp-styleguide/]
> Note
> 所有在 Google 的 C++ 風格指南中所禁止的事情,如未明確說明,也同樣不能在Objective-C++ 中使用。
本文檔的目的在于為所有的 Mac OS X 的代碼提供編碼指南及實踐。許多準則是在實際的項目和小組中經過長期的演化、驗證的。Google 開發的開源項目遵從本指南的要求。
Google 已經發布了遵守本指南開源代碼,它們屬于 [Google Toolbox for Mac project](http://code.google.com/p/google-toolbox-for-mac/) [http://code.google.com/p/google-toolbox-for-mac/] 項目(本文以縮寫 GTM 指代)。GTM 代碼庫中的代碼通常為了可以在不同項目中復用。
注意,本指南不是 Objective-C 教程。我們假定讀者對 Objective-C 非常熟悉。如果你剛剛接觸 Objective-C 或者需要溫習,請閱讀 [The Objective-C Programming Language](http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.html) [http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/index.html] 。
## 例子
都說一個例子頂上一千句話,我們就從一個例子開始,來感受一下編碼的風格、留白以及命名等等。
一個頭文件的例子,展示了在 `@interface` 聲明中如何進行正確的注釋以及留白。
// 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
一個源文件的例子,展示了 `@implementation` 部分如何進行正確的注釋、留白。同時也包括了基于引用實現的一些重要方法,如 `getters` 、 `setters` 、 `init` 以及 `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
不要求在 `@interface`、`@implementation` 和 `@end` 前后空行。如果你在 `@interface` 聲明了實例變量,則須在關括號 `}` 之后空一行。
除非接口和實現非常短,比如少量的私有方法或橋接類,空行方有助于可讀性。
- Google 開源項目風格指南 (中文版)
- C++ 風格指南
- 0. 扉頁
- 1. 頭文件
- 2. 作用域
- 3. 類
- 4. 來自 Google 的奇技
- 5. 其他 C++ 特性
- 6. 命名約定
- 7. 注釋
- 8. 格式
- 9. 規則特例
- 10. 結束語
- Objective-C 風格指南
- Google Objective-C Style Guide 中文版
- 留白和格式
- 命名
- 注釋
- Cocoa 和 Objective-C 特性
- Cocoa 模式
- Python 風格指南
- Google Python 風格指南 - 中文版
- 背景
- Python語言規范
- Python風格規范
- 臨別贈言
- JSON 風格指南
- 簡介
- 定義
- 一般準則
- 屬性名準則
- 屬性值準則
- 屬性值數據類型
- JSON結構和保留屬性名
- 頂級保留屬性名稱
- data對象的保留屬性名
- 用于分頁的保留屬性名
- 用于鏈接的保留屬性名
- 錯誤對象中的保留屬性名
- 屬性順序
- 示例
- 附錄