代码规范

头文件#import的顺序

写法模板
import <系统库>
import <第三方库>
import “其他类”
尽量按照先系统类 第三方类 自己写的类顺序导入 中间不能有空格

建议的写法

import 
import "ViewController.h"
import "IOSMD5.h"

不建议的写法

import "ViewController.h"
import "IOSMD5.h"
import 

代码组织

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

#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}

空格

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

建议的写法

if (user.isHappy) { 
    //Do something
} else { 
    //Do something else
}

不建议的写法

if (user.isHappy)
{ 
    //Do something
}
else 
{ 
    //Do something else
}

方法参数之间换行

应该避免以冒号对齐的方式来调用方法。因为有时方法签名可能有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 
                             }];

@Class的写法

建议的写法

@class SecondViewController, ChangeViewController;

不建议的写法

@class SecondViewController;
@class ChangeViewController;

@Interface的写法

写法模板
@interface 类名 : 父类 <协议1, 2="">
@interface和类名中间一个空格
类名后紧跟:之后空格加上父类协议之间用,空格分割

建议的写法

@interface AppDelegate : UIResponder 

不建议的写法

@interface AppDelegate:UIResponder

@protocol的写法

写法的模板
@protocol 协议的名称 <协议1, 2="">
@potocol和协议的名称有空格 协议的名称和其他协议有空格 其他协议之间有空格

建议的写法

@protocol AHShoppingCartPreparePayProtocal 

不建议的写法

@protocol AHShoppingCartPreparePayProtocal

@property的写法

@property (关键词, 关键词) 类 *变量名称;
关键词用,空格分割 类前后空格

建议的写法

@property (nonatomic, strong) UIButton *userSettingBtn;

不建议的写法

@property(nonatomic, strong) UIButton * userSettingBtn;

h头文件方法写法

写法模板
@interface
方法的参数在一排显示
方法之间保留一行
第一个方法和@interface保留空行
最后一个方法和@end保留空行

建议的写法

@interface AHProductShowViewController : UIViewController

- (void)setWidgetInfo:(AHWidgetInfo *)widgetInfo;

@end

不建议的写法

@interface AHProductShowViewController : UIViewController
- (void)setWidgetInfo:(AHWidgetInfo *)widgetInfo;
@end

点符号语法

点语法是一种很方便封装访问方法调用的方式。当你使用点语法时,通过使用getter或setter方法,属性仍然被访问或修改。
点语法应该总是被用来访问和修改属性,因为它使代码更加简洁。[] 符号更偏向于用在其他例子。

建议的写法

NSInteger arrayCount = self.array.count;
view.backgroundColor = [UIColor orangeColor];
[UIApplication sharedApplication].delegate;

不建议的写法

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

注释一定要写

自己管理的类一定注释属性用途 方法的用途 参数的说明
属性如果设置默认值 一定注明默认值是什么
如果方法内部存在逻辑判断 方法跳转 一定注释判断用法 方法跳转用法
除了初始化操作
其他声明变量 赋值 判断 应该注明注释用途

注释的写法

Class类注释

/**
 控制的 cell
 */
@interface CRMControlTableViewCell : UITableViewCell

property属性的注释

/** 
  已经选择的字段 
  */
@property (nonatomic, strong) NSMutableArray *selecteds;

方法的注释

如果有返回值 请加上return

/**
 @method isCustomerContantImportByName:phone:
 @abstract  根据姓名和电话号查询顾客
 @discussion 根据姓名和电话号查询顾客
 @param name customer's name phone customer's phone
 @result 返回一组一个字符串(男/女/nil)对象
 */
+ (NSString *)isCustomerContantImportByName:(NSString *)name phone:(NSString *)phoneNumber;

Block注释

/**
 @method syncDeviceUpdateTimeSuccess:failure:
 @abstract 客户端修改设备更新时间的接口
 @discussion 客户端修改设备更新时间的接口
 @param onSuccess 成功回调
 @param onFailure 失败回调
 */
- (void)syncDeviceUpdateTimeSuccess:(void (^)())onSuccess failure:(void (^)(NSError *))onFailure;

NSUM 的注释

/*!
  当前输入框的状态
  - ATFVEditStateNormal  : 默认 还没有输入任何的文字
  - ATFVEditStateEditing  :  正在进行输入
  - ATFVEditStateEdited   :  输入完毕
  - ATFVEditStateNoEdit  :  不允许编辑
  */
typedef NS_ENUM(NSUInteger , ATFVEditState) {
    ATFVEditStateNormal,
    ATFVEditStateEditing,
    ATFVEditStateEdited,
    ATFVEditStateNoEdit
};

计算符号两边要有空格

比如 + - * / =等运算符左右有空格

建议的写法

x = 1 + 2;

不建议的写法

x=1+2;

控件命名的规范

对于命名一定不要简写 那篇很长的单词 但是一些单词就是简写的除外 比如WTO RMB
UILabel结尾加上Label;
UIImageView结尾记上ImageView
等等让其他的编程人员看名字就知道变量的用法 和属于什么控件

建议的写法

@property (nonatomic, strong) UILabel *nameLabel;

不建议的写法

@property (nonatomic, strong) UILabel *name;

对于#define宏命名

单词全部的大写 单词之间用_分割

建议的写法

#define WEB_ROOT_IP @"qa.amway.com.cn"

不建议的写法

#define kAHPaymentCenterRelease @"NO"

对象调用方法要留空格

建议的写法

[[NSNotificationCenter defaultCenter] removeObserver:self];

不建议的写法

[[NSNotificationCenter defaultCenter]removeObserver:self];

对于局部的变量尽量的初始化

局部的变量要初始化 属性有默认的值

建议的写法

int index = 0;

不建议的写法

int index;

变量名的规范

一定要使用驼峰的命名

建议的写法

CRMNetService *netService = [CRMNetService sharedInstance];

不建议的写法

CRMNetService *netservice = [CRMNetService sharedInstance];

使用NSUserDefaults要先创建

因为我们用到NSUserDefaults无非是保存和读取 事先的创建一个对象 可以精简代码
当执行方法很多 用变量替换

建议的写法

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *lastSyncTime = [userDefaults valueForKey:[self lastSyncTimeKey]];

不建议的写法

[[NSUserDefaults standardUserDefaults] valueForKey:[self lastSyncTimeKey]];

条件语句

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

建议的写法

if (!error) {
    return success;
}

不建议的写法

if (!error) return success;

黄金路径

当使用条件语句编码时,左手边的代码应该是"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
}

类名加上前缀避免冲突

因为团队的合作 可能会出现大家想到一样的名字或者添加第三方库引入和第三方库名字一样
尽量加上前缀。
如果只针对工程就使用工程的缩写
比如自己个人的第三方库就加上自己名字或者昵称的缩写

建议的写法

@interface CRMAddEditGroupViewController : UIViewController

不建议的写法

@interface AddEditGroupViewController : UIViewController

尽可能使用不可变的对象

对于OC存在很多可变的对象 比如NSMutableString NSMutableArray NSMutableDictionary等等
对于一些不允许改变的直接使用不可变对象
可以节省对象开支 还可以防止别人修改数据造成bug

建议的写法

NSArray *sexList = @[@"男",@"女"];

不建议的写法

NSMutableArray *sexList = [NSMutableArray arrayWithArray:@[@"男",@"女"]];

对于一些自己不确定的可以使用try catch

对于不知道后台返回什么类型的 可以使用try catch

建议的写法

int index = 0;
@try {
    NSArray *array = obj[@"list"];
    index = [array.firstObject intValue];
}
@catch { }

不建议的写法

int index = 0;
NSArray *array = obj[@"list"];
if (array.count > 0) {
    index = [array.firstObject intValue];
}

使用dispatch_once来创建单例

建议的写法

+ (instancetype)sharedInstance { 
    static id sharedInstance = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
        sharedInstance = [[self alloc] init]; 
    });
    return sharedInstance;
}

便利的写法

如果只需要便利数组和字典的写法用for in

建议的写法

for (NSString *name in names) {

}

不建议的写法

for (int i = 0; i < names.lenght; i ++) {

}

字典的元素垂直写

建议的写法

NSDictionary *dictionary = @{
                                                   @"a" : @"",
                                                   @"b" : @"",
                                                   @"c" : @""
                                                 };

不建议的写法

NSDictionary *dictionary = @{@"a" : @"", @"b" : @"", @"c" : @"" }

你可能感兴趣的:(代码规范)