Google Objective-C Style Guide 中文版

版本
2.36 原作者 * Mike Pinkerton * Greg Miller * Dave MacLachlan

翻译 * ewangke * Yang.Y

项目主页 * Google Style Guide * Google 开源项目风格指南 - 中文版

译者的话

ewanke

一直想翻译这个 style guide ,终于在周末花了7个小时的时间用vim敲出了HTML。很多术语的翻译很难,平时看的中文技术类书籍有限,对很多术语的中文译法不是很清楚,难免有不恰当之处,请读者指出并帮我改进:王轲 ”ewangke at gmail.com” 2011.03.27

Yang.Y

对 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 * Google’s Open Source C++ Style Guide

Note 所有在 Google 的 C++ 风格指南中所禁止的事情,如未明确说明,也同样不能在Objective-C++ 中使用。 本文档的目的在于为所有的 Mac OS X 的代码提供编码指南及实践。许多准则是在实际的项目和小组中经过长期的演化、验证的。Google 开发的开源项目遵从本指南的要求。 Google 已经发布了遵守本指南开源代码,它们属于 Google Toolbox for Mac project 项目(本文以缩写 GTM 指代)。GTM 代码库中的代码通常为了可以在不同项目中复用。 注意,本指南不是 Objective-C 教程。我们假定读者对 Objective-C 非常熟悉。如果你刚刚接触 Objective-C 或者需要温习,请阅读 The Objective-C Programming Language 。

例子

都说一个例子顶上一千句话,我们就从一个例子开始,来感受一下编码的风格、留白以及命名等等。 一个头文件的例子,展示了在 @interface 声明中如何进行正确的注释以及留白。 1. // Foo.h 2. // AwesomeProject 3. // 4. // Created by Greg Miller on 6/13/08. 5. // Copyright 2008 Google, Inc. All rights reserved. 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 header) 12. // MUST be commented. Comments must also be adjacent to the object they're 13. // documenting. 14. // 15. // (no blank line between this comment and the interface) 16. @interface Foo : NSObject { 17. @private 18. NSString *bar_; 19. NSString *bam_; 20. } 21.   22. // Returns an autoreleased instance of Foo. See -initWithBar: for details 23. // about |bar|. 24. + (id)fooWithBar:(NSString *)bar; 25.   26. // Designated initializer. |bar| is a thing that represents a thing that 27. // does a thing. 28. - (id)initWithBar:(NSString *)bar; 29.   30. // Gets and sets |bar_|. 31. - (NSString *)bar; 32. - (void)setBar:(NSString *)bar; 33.   34. // Does some work with |blah| and returns YES if the work was completed 35. // successfully, and NO otherwise. 36. - (BOOL)doWorkWithBlah:(NSString *)blah; 37.   38. @end

一个源文件的例子,展示了 @implementation 部分如何进行正确的注释、留白。同时也包括了基于引用实现的一些重要方法,如 getters 、 setters 、 init 以及 dealloc 。 1. // 2. // Foo.m 3. // AwesomeProject 4. // 5. // Created by Greg Miller on 6/13/08. 6. // Copyright 2008 Google, Inc. All rights reserved. 7. // 8.   9. #import "Foo.h" 10.   11.   12. @implementation Foo 13.   14. + (id)fooWithBar:(NSString *)bar { 15. return [[[self alloc] initWithBar:bar] autorelease]; 16. } 17.   18. // Must always override super's designated initializer. 19. - (id)init { 20. return [self initWithBar:nil]; 21. } 22.   23. - (id)initWithBar:(NSString *)bar { 24. if ((self = [super init])) { 25. bar_ = [bar copy]; 26. bam_ = [[NSString alloc] initWithFormat:@"hi %d", 3]; 27. } 28. return self; 29. } 30.   31. - (void)dealloc { 32. [bar_ release]; 33. [bam_ release]; 34. [super dealloc]; 35. } 36.   37. - (NSString *)bar { 38. return bar_; 39. } 40.   41. - (void)setBar:(NSString *)bar { 42. [bar_ autorelease]; 43. bar_ = [bar copy]; 44. } 45.   46. - (BOOL)doWorkWithBlah:(NSString *)blah { 47. // ... 48. return NO; 49. } 50.   51. @end

不要求在 @interface、@implementation 和 @end 前后空行。如果你在 @interface 声明了实例变量,则须在关括号 } 之后空一行。 除非接口和实现非常短,比如少量的私有方法或桥接类,空行方有助于可读性。

原文: https://zh-google-styleguide.readthedocs.io/en/latest/google-objc-styleguide/