iOS 代码规范文档

2018-07-31  本文已影响137人  喜相逢v5

iOS 代码规范文档

[toc]

修订

序号 版本 修订人 修订日期 备注
1 1.0 李俊杰 2018-07-31 起草
2

概述

文件及目录管理规范

代码注释规范

 ```   
//
//  AppDelegate.m //文件名
//  UniXin // 项目名
//
//  Created by JunJie on 2018/7/31. //创建人及日期
//  Copyright © 2018年 Longfor Properties Co. Ltd. All rights reserved. //版权
//


 - 方法前面的注释遵循以下格式:如果某项没有,不显示。公共接口的每个方法,都应该有注释来解释它的作用、参数、返回值以及其它影响(建议放到description中)。
 
  ``` 
   /**
  @description 根据轮子和车架创建一个汽车
  @param 轮子
  @param 车架
  @return 一个汽车
  */
  
  - (Car *)createCar:(NSString *)wheel carFrame:(NSString *)carFrame;
  ```
 
 - 使用 | 来引用注释中的变量名及符号名而不是使用引号。这会避免二义性,尤其是当符号是一个常用词汇,这使用语句读起来很糟糕。例如,对于符号 count :
例如:
// Sometimes we need |count| to be less than zero.(有时候我们需要|数量|的值小于0)

 - 不必每行都加注释,在关键代码处,5行代码左右处做注释;

 - 协议及模块之间需要加#pragma marks - 来区分

## 编码排版格式规范

### 空行

#### .h文件中的空行
- 文件说明与头文件包涵(#import)之间空1行

//
//  ViewController.h
//  UniXin
//
//  Created by JunJie on 2018/7/31.
//  Copyright © 2018年 Longfor Properties Co. Ltd. All rights reserved.
//

#import <UIKit/UIKit.h>
  ```
#import <UIKit/UIkit.h>
#import <Foundation/Foundation.h>

#import "GODOtherClass.h"
#import "GODOtherModel.h"

@class GODAnotherClass;
@protocol GODAnotherProtocol;

@interface GODExample : NSObject {

    NSString *instanceVariable;
}

@propety (nonatomic, strong) GODOtherModel *otherModel;

- (void)test;

@end

.m中的空行

#pragma mark - private methods

- (void)privateMethod1 {

  NSString *innerVariable1 = nil;
  NSInteger innerVariable2 = 100;

  BOOL condition = YES;

  if (condition) {
      NSLog(@"xxxxx");
  }

  //do real logic
}

关于空格

关于BOOL值

命名规范

保留字

Objective-c语言的保留字或关键词应全部使用小写字母,除下表中保留字外,privateprotectedpublic 、在类型说明中也作为保留字使用。还有nonatomanic ,retain ,readwrite , readonly 等也有特殊的使用场合。下表中的关键字不允许作为变量名使用。

_Bool _Complex _Imaginary auto break bycopy
return self short` restrict typeof union
void volatile while property public private
byref case char const continue default
do double const enum extern float
for got if in inline inout
int long oneway else out registre
static struct super switch signed sizeof

文件

方法

变量

typedef enum CGPathDrawingMode CGPathDrawingMode;     
/* Drawing modes for text. */     
enum CGTextDrawingMode  { 
     kCGTextFill,
     kCGTextStroke, 
     kCGTextFillStroke, 
     kCGTextInvisible, 
     kCGTextFillClip, 
     kCGTextStrokeClip, 
     kCGTextFillStrokeClip, 
     kCGTextClip 
}; 

常量

static NSString *kGODCustomerCellID = @"GODCustomerCellID";

define kGODCustomerCellHeight 200.f

-  一些常量前加特殊前缀,可以作为不同常量的区分

UserDefaultsKey 的变量前加UDKEY_,    
NotificationNameKey 前面加NNKEY_,    
DictionaryKey 前面加DICTKEY_, 
### 属性

- 修饰符的顺序:nonatomic > strong/weak/retain/copy > readonly > getter/setter=
- NSString类型的属性使用copy进行修饰。
- Block类型的属性使用copy进行修饰。
- delegate类型的属性使用weak进行修饰,系统会在dealloc的时候自动设置成nil。以避免循环引用。
- 尽量避免使用原子性修饰,否则会因为同步操作产生额外的开销。默认的设置是原子的。 

### 类

- 所有的类名,协议名(Protocol)均以大写字母开头,多单词组合时,后面的单词首字母大写。类、协议名必须是有意义的。
- 继承自UIView的类以View结尾。em. UserInformationView
- 继承自ViewController的类以viewController结尾。em. LoginViewController
- 所有保存数据的实体以Model结尾。em. userModel
- 分类(类别)命名:与类命名相同,此外需添加要扩展的类名和 `+`。 em. NSString+URLEncoding
- 协议(委托)命名:与类命名相同,此外需添加`protocol`后缀。 em. UINavigationControllerProtocol

@protocol GODExampleProtocol <NSObject>

  @required
  - (NSInteger)numberOfTableViewIndexPath:(NSIndexPath *)indexPath;
  
  @optional
  - (void)didSelectedIndexPath:(NSIndexPath *)indexPath;

@end



### 其他
- 每个方法不能超过100行,超过100行的代码必须拆分。
- 嵌套语句不能超过3层;超过3层必须优化和拆分。
- 每个类文件行数建议在500行左右,不能超过1000行,超过1000行必须考虑抽象类来重构代码。
- 代码行度最大为100列(C++的是80),可以在xcode的Text Editing中修改。
- 尽可能保证 .h文件的简洁性,可以不公开的API就不要公开了,写在实现文件中即可。
- 及时删除无用的注释和无用的代码。
- 代码中不应该出现控件的绝对坐标,应尽可能使用相对坐标。
- 写`delegate` 的时候类型应该为`weak` 弱引用,以避免循环引用。
上一篇 下一篇

猜你喜欢

热点阅读