关于Objective C编码规范,这些你必定要会

##背景 这里有些关于编码风格Apple官方文档,若是有些东西没有说起,能够在如下文档来查找更多细节:html

##语言 应该使用US英语。 应该: UIColor *myColor = [UIColor whiteColor];安全

不该该: UIColor *myColour = [UIColor whiteColor];bash

代码组织

在函数分组和protocol/delegate实现中使用#pragma mark -来分类方法,要遵循如下通常结构:数据结构

#pragma mark - Lifecycle 

- (instancetype)init {} 
- (void)dealloc {} 
- (void)viewDidLoad {} 
- (void)viewWillAppear:(BOOL)animated {} 
- (void)didReceiveMemoryWarning {} 

#pragma mark 

- Custom Accessors 
- (void)setCustomProperty:(id)value {} 
- (id)customProperty {} 

#pragma mark - IBActions 

- (IBAction)submitData:(id)sender {} 

#pragma mark - Public 

- (void)publicMethod {} 

#pragma mark - Private 

- (void)privateMethod {} 

#pragma mark 

- Protocol conformance 

#pragma mark - UITextFieldDelegate 
#pragma mark - UITableViewDataSource 
#pragma mark 

- UITableViewDelegate 

#pragma mark - NSCopying 

- (id)copyWithZone:(NSZone *)zone {} 

#pragma mark - NSObject 

- (NSString *)description {}
复制代码

空格

  • 缩进使用4个空格,确保在Xcode偏好设置来设置。(raywenderlich.com使用2个空格)
  • 方法大括号和其余大括号(if/else/switch/while 等.)老是在同一行语句打开但在新行中关闭。

应该:app

if (user.isHappy){ 
    //Do something 

} else { 
    //Do something else 
}
复制代码

不该该:iphone

if (user.isHappy)
{ 
  //Do something 
} else { 
  //Do something else 
}
复制代码

在方法之间应该有且只有一行,这样有利于在视觉上更清晰和更易于组织。在方法内的空白应该分离功能,但一般都抽离出来成为一个新方法。 优先使用auto-synthesis。但若是有必要,@synthesize和@dynamic应该在实现中每一个都声明新的一行。 应该避免以冒号对齐的方式来调用方法。由于有时方法签名可能有3个以上的冒号和冒号对齐会使代码更加易读。请不要这样作,尽管冒号对齐的方法包含代码块,由于Xcode的对齐方式令它难以辨认。

应该:

// blocks are easily readable
[UIView animateWithDuration:1.0 animations:^{ 
  
  // something 

} completion:^(BOOL finished) {

   // something 

}];
复制代码

不该该:

// colon-aligning makes the block indentation hard to read 
[UIView animateWithDuration:1.0 animations:^{ 

                                // something  

                                }completion:^(BOOL finished) { 

                                // something  

                                }];
复制代码

注释

当须要注释时,注释应该用来解释这段特殊代码为何要这样作。任何被使用的注释都必须保持最新或被删除。 通常都避免使用块注释,由于代码尽量作到自解释,只有当断断续续或几行代码时才须要注释。例外:这不该用在生成文档的注释 Apple命名规则尽量坚持,特别是与这些相关的memory management rules。 长的,描述性的方法和变量命名是好的。

应该: UIButton *settingsButton;

不该该: UIButton *setBut;

三个字符前缀应该常常用在类和常量命名,但在Core Data的实体名中应被忽略。对于官方的raywenderlich.com书、初学者工具包或教程,前缀’RWT’应该被使用。

常量应该使用驼峰式命名规则,全部的单词首字母大写和加上与类名有关的前缀。 应该: static NSTimeInterval const RWTTutorialViewControllerNavigationFadeAnimationDuration = 0.3;

不该该: static NSTimeInterval const fadetime = 1.7;

属性也是使用驼峰式,但首单词的首字母小写。对属性使用auto-synthesis,而不是手动编写@synthesize语句,除非你有一个好的理由。 应该: @property (strong, nonatomic) NSString *descriptiveVariableName;

不该该: id varnm;

下划线

当使用属性时,实例变量应该使用self.来访问和改变。这就意味着全部属性将会视觉效果不一样,由于它们前面都有self.。 但有一个特例:在初始化方法里,实例变量(例如,_variableName)应该直接被使用来避免getters/setters潜在的反作用。 局部变量不该该包含下划线。

方法

在方法签名中,应该在方法类型(-/+ 符号)以后有一个空格。在方法各个段之间应该也有一个空格(符合Apple的风格)。在参数以前应该包含一个具备描述性的关键字来描述参数。 “and”这个词的用法应该保留。它不该该用于多个参数来讲明,就像initWithWidth:height如下这个例子: 应该:

- (void)setExampleText:(NSString *)text image:(UIImage *)image; 
- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag; 
- (id)viewWithTag:(NSInteger)tag; 
- (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height;
复制代码

不该该:

-(void)setT:(NSString *)text i:(UIImage *)image; 
- (void)sendAction:(SEL)aSelector :(id)anObject :(BOOL)flag; 
- (id)taggedView:(NSInteger)tag; 
- (instancetype)initWithWidth:(CGFloat)width andHeight:(CGFloat)height; 
- (instancetype)initWith:(int)width and:(int)height; // Never do this. 
复制代码

变量

变量尽可能以描述性的方式来命名。单个字符的变量命名应该尽可能避免,除了在for()循环。 星号表示变量是指针。例如:NSString *text 既不是 NSString* text 也不是 NSString * text,除了一些特殊状况下常量。 私有变量应该尽量代替实例变量的使用。尽管使用实例变量是一种有效的方式,但更偏向于使用属性来保持代码一致性。 经过使用’back’属性(_variable,变量名前面有下划线)直接访问实例变量应该尽可能避免,除了在初始化方法(init, initWithCoder:, 等…),dealloc 方法和自定义的setters和getters。 应该:

@interface RWTTutorial : NSObject 
@property (strong, nonatomic) NSString *tutorialName; 
@end 
复制代码

不该该:

@interface RWTTutorial : NSObject { 
  NSString *tutorialName; 
}
复制代码

属性特性

全部属性特性应该显式地列出来,有助于新手阅读代码。属性特性的顺序应该是storage、atomicity,与在Interface Builder链接UI元素时自动生成代码一致。 应该:

@property (weak, nonatomic) IBOutlet UIView *containerView; 
@property (strong, nonatomic) NSString *tutorialName;
复制代码

不该该:

@property (nonatomic, weak) IBOutlet UIView *containerView; 
@property (nonatomic) NSString *tutorialName;
复制代码

NSString应该使用copy而不是strong的属性特性。 为何?即便你声明一个NSString的属性,有人可能传入一个NSMutableString的实例,而后在你没有注意的状况下修改它。 应该: @property (copy, nonatomic) NSString *tutorialName;

不该该: @property (strong, nonatomic) NSString *tutorialName;

点符号语法

点语法是一种很方便封装访问方法调用的方式。当你使用点语法时,经过使用getter或setter方法,属性仍然被访问或修改。想了解更多,阅读: developer.apple.com/library/ios… 。 点语法应该老是被用来访问和修改属性,由于它使代码更加简洁。[]符号更偏向于用在其余例子。 应该:

objc NSInteger arrayCount = [self.array count]; 
view.backgroundColor = [UIColor orangeColor]; 
[UIApplication sharedApplication].delegate;
复制代码

不该该:

NSInteger arrayCount = self.array.count;
[view setBackgroundColor:[UIColor orangeColor]]; 
UIApplication.sharedApplication.delegate;
复制代码

字面值

NSString、NSDictionary、NSArray和NSNumber的字面值应该在建立这些类的不可变实例时被使用。请特别注意nil值不能传入NSArray和NSDictionary字面值,由于这样会致使crash。 应该:

NSArray *names = @[@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul"];
NSDictionary *productManagers = @{@"iPhone": @"Kate", @"iPad": @"Kamal", @"Mobile Web": @"Bill"}; 
NSNumber *shouldUseLiterals = @YES; 
NSNumber *buildingStreetNumber = @10018;
复制代码

不该该:

NSArray *names = [NSArray arrayWithObjects:@"Brian", @"Matt", @"Chris", @"Alex", @"Steve", @"Paul", nil]; 
NSDictionary *productManagers = [NSDictionary dictionaryWithObjectsAndKeys: @"Kate", @"iPhone", @"Kamal", @"iPad", @"Bill", @"Mobile Web", nil]; 
NSNumber *shouldUseLiterals = [NSNumber numberWithBool:YES]; 
NSNumber *buildingStreetNumber = [NSNumber numberWithInteger:10018];
复制代码

常量

常量是容易重复被使用和无需经过查找和代替就能快速修改值。常量应该使用static来声明而不是使用#define,除非显式地使用宏。 应该:

static NSString * const RWTAboutViewControllerCompanyName = @"RayWenderlich.com"; 
static CGFloat const RWTImageThumbnailHeight = 50.0;
复制代码

不该该:

#define CompanyName @"RayWenderlich.com" #define thumbnailHeight 2 
复制代码

枚举类型

当使用enum时,推荐使用新的固定基本类型规格,由于它有更强的类型检查和代码补全。如今SDK有一个宏NS_ENUM()来帮助和鼓励你使用固定的基本类型。 例如:

typedef NS_ENUM(NSInteger, RWTLeftMenuTopItemType) { 
  RWTLeftMenuTopItemMain, 
  RWTLeftMenuTopItemShows, 
  RWTLeftMenuTopItemSchedule 
};
复制代码

你也能够显式地赋值(展现旧的k-style常量定义):

typedef NS_ENUM(NSInteger, RWTGlobalConstants) { 
  RWTPinSizeMin = 1, 
  RWTPinSizeMax = 5, 
  RWTPinCountMin = 100, 
  RWTPinCountMax = 500, 
};
复制代码

旧的k-style常量定义应该避免除非编写Core Foundation C的代码。 不该该:

enum GlobalConstants { 
  kMaxPinSize = 5, 
  kMaxPinCount = 500, 
};
复制代码

Case语句

大括号在case语句中并非必须的,除非编译器强制要求。当一个case语句包含多行代码时,大括号应该加上。

switch (condition){ 
  case 1: 
  // ...  
  break; 

  case 2: { 
     // ...  
     // Multi-line example using braces  
     break; 
    } 
  case 3: 
  // ...  
  break; 

  default: 
  // ...  
  break; 

}
复制代码

有不少次,当相同代码被多个cases使用时,一个fall-through应该被使用。一个fall-through就是在case最后移除’break’语句,这样就可以容许执行流程跳转到下一个case值。为了代码更加清晰,一个fall-through须要注释一下。

switch (condition) { 
  case 1: 
  // ** fall-through! **  
  case 2: 
  // code executed for values 1 and 2  
  break; 
  default:
   // ...  break; 
}
复制代码

当在switch使用枚举类型时,’default’是不须要的。例如:

RWTLeftMenuTopItemType menuType = RWTLeftMenuTopItemMain; 
switch (menuType) { 
  case RWTLeftMenuTopItemMain: 
  // ...  
  break; 
  case RWTLeftMenuTopItemShows: 
  // ...  
  break; 
  case RWTLeftMenuTopItemSchedule: 
  // ...  
  break; 
}
复制代码

私有属性

私有属性应该在类的实现文件中的类扩展(匿名分类)中声明,命名分类(好比RWTPrivate或private)应该从不使用除非是扩展其余类。匿名分类应该经过使用+Private.h文件的命名规则暴露给测试。

例如:

@interface RWTDetailViewController () 
@property (strong, nonatomic) GADBannerView *googleAdView; 
@property (strong, nonatomic) ADBannerView *iAdView; 
@property (strong, nonatomic) UIWebView *adXWebView; 

@end 
复制代码

布尔值

Objective-C使用YES和NO。由于true和false应该只在CoreFoundation,C或C++代码使用。既然nil解析成NO,因此没有必要在条件语句比较。不要拿某样东西直接与YES比较,由于YES被定义为1和一个BOOL能被设置为8位。 这是为了在不一样文件保持一致性和在视觉上更加简洁而考虑。 应该:

if (someObject) {} 
if (![anotherObject boolValue]) {}
复制代码

不该该:

if (someObject == nil) {} 
if ([anotherObject boolValue] == NO) {} 
if (isAwesome == YES) {} // Never do this. 
if (isAwesome == true) {} // Never do this. 
复制代码

若是BOOL属性的名字是一个形容词,属性就能忽略”is”前缀,但要指定get访问器的惯用名称。例如: @property (assign, getter=isEditable) BOOL editable;

文字和例子从这里引用Cocoa Naming Guidelines。

条件语句

条件语句主体为了防止出错应该使用大括号包围,即便条件语句主体可以不用大括号编写(如,只用一行代码)。这些错误包括添加第二行代码和指望它成为if语句;还有,even more dangerous defect可能发生在if语句里面一行代码被注释了,而后下一行代码不知不觉地成为if语句的一部分。除此以外,这种风格与其余条件语句的风格保持一致,因此更加容易阅读。 应该:

if (!error) {  
  return success; 
}
复制代码

不该该:

if (!error)  
return success;
复制代码

if (!error) return success;

三元操做符

当须要提升代码的清晰性和简洁性时,三元操做符?:才会使用。单个条件求值经常须要它。多个条件求值时,若是使用if语句或重构成实例变量时,代码会更加易读。通常来讲,最好使用三元操做符是在根据条件来赋值的状况下。 Non-boolean的变量与某东西比较,加上括号()会提升可读性。若是被比较的变量是boolean类型,那么就不须要括号。 应该:

NSInteger value = 5; 
result = (value != 0) ? x : y; 

BOOL isHorizontal = YES; 
result = isHorizontal ? x : y;
复制代码

不该该: result = a > b ? x = c > d ? c : d : y;

Init方法

Init方法应该遵循Apple生成代码模板的命名规则,返回类型应该使用instancetype而不是id。

- (instancetype)init { 
  self = [super init]; 
  if (self) { // ...  } 
  return self;
 }
复制代码

类构造方法

当类构造方法被使用时,它应该返回类型是instancetype而不是id。这样确保编译器正确地推断结果类型。

@interface Airplane 
+ (instancetype)airplaneWithType:(RWTAirplaneType)type; 
@end 
复制代码

关于更多instancetype信息,请查看NSHipster.com。

CGRect函数

当访问CGRect里的x, y, width, 或 height时,应该使用CGGeometry函数而不是直接经过结构体来访问。引用Apple的CGGeometry: 在这个参考文档中全部的函数,接受CGRect结构体做为输入,在计算它们结果时隐式地标准化这些rectangles。所以,你的应用程序应该避免直接访问和修改保存在CGRect数据结构中的数据。相反,使用这些函数来操纵rectangles和获取它们的特性。

应该:

CGRect frame = self.view.frame; 
CGFloat x = CGRectGetMinX(frame); 
CGFloat y = CGRectGetMinY(frame); 
CGFloat width = CGRectGetWidth(frame); 
CGFloat height = CGRectGetHeight(frame); 
CGRect frame = CGRectMake(0.0, 0.0, width, height);
复制代码

不该该:

CGRect frame = self.view.frame; 
CGFloat x = frame.origin.x; 
CGFloat y = frame.origin.y; 
CGFloat width = frame.size.width; 
CGFloat height = frame.size.height; 
CGRect frame = (CGRect){ .origin = CGPointZero, .size = frame.size };
复制代码

黄金路径

当使用条件语句编码时,左手边的代码应该是”golden” 或 “happy”路径。也就是不要嵌套if语句,多个返回语句也是OK。 应该:

- (void)someMethod {
   if (![someOther boolValue]) { 
    return; 
  } 
  //Do something important 
}
复制代码

不该该:

- (void)someMethod {
  if ([someOther boolValue]) {
    //Do something important
  }
}
复制代码

错误处理

当方法经过引用来返回一个错误参数,判断返回值而不是错误变量。 应该:

NSError *error;
if (![self trySomethingWithError:&error]) {
  // Handle Error
}
复制代码

不该该:

NSError *error;
[self trySomethingWithError:&error];
if (error) {
  // Handle Error
}
复制代码

在成功的状况下,有些Apple的APIs记录垃圾值(garbage values)到错误参数(若是non-NULL),那么判断错误值会致使false负值和crash。

单例模式

单例对象应该使用线程安全模式来建立共享实例。

+ (instancetype)sharedInstance {
  static id sharedInstance = nil;

  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedInstance = [[self alloc] init];
  });

  return sharedInstance;
}
复制代码

这会防止possible and sometimes prolific crashes。

换行符

换行符是一个很重要的主题,由于它的风格指南主要为了打印和网上的可读性。 例如:

self.productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
复制代码

一行很长的代码应该分红两行代码,下一行用两个空格隔开。

self.productsRequest = [[SKProductsRequest alloc]
  initWithProductIdentifiers:productIdentifiers];
复制代码

Xcode工程

物理文件应该与Xcode工程文件保持同步来避免文件扩张。任何Xcode分组的建立应该在文件系统的文件体现。代码不只是根据类型来分组,并且还能够根据功能来分组,这样代码更加清晰。 尽量在target的Build Settings打开”Treat Warnings as Errors,和启用如下additional warnings。若是你须要忽略特殊的警告,使用Clang’s pragma feature。

【注】本文翻译自github的objective-c-style-guide,隐去了部分介绍内容,着重点在OC编码规范知识点。

相关文章
相关标签/搜索