iOS 编码规范
in 技术 with 0 comment

iOS 编码规范

in 技术 with 0 comment

iOS 编码规范

v0.0.1,2019.1.3

本文档并不会涉及到代码优化:NSInteger 替代 int,NSArray 类型属性需要用 copy 特性等。

本文档为纯编码规范,主要是为了统一编码习惯。包含核心原则,命名规范,通用规范,代码组织,注释五个主题。

更好的排版可以查看我的 Github

核心原则

1. 语言,使用 US 英语命名

文件名,类,方法,宏,ImageSet,Localizable.string Key,配置,#pragma mark,model 字段

Colour,manzengId,颜色 //不建议(英式英语,拼音,中文)
Color //推荐

#pragma mark - 懒加载 //不建议
#pragma mark - Property Method //推荐
2. 代码风格

简单易懂,逻辑清晰。清晰永远比简洁更重要

insertObject:at: //不建议
insertObject:atIndex: //推荐

setBGColor: //不建议
setBackgroundColor:  //推荐

[button setTitle:@"title" forState:0]; //不建议
[button setTitle:@"title" forState:UIControlStateNormal]; //推荐

result = a > b ? x = c > d ? c : d : y; //不建议(多个表达式嵌套)
//推荐
msg = code > 0 ? @"Error" : @"Success";
result = reachable > NotReachable ? msg : @"Fail";

命名规范

1. 遵从驼峰命名法
2. ImageSet
//不建议
bg
backImage
        
//推荐
product_detail_price_background
nav_bar_back
3. Localizable.string
//不建议
EDIT
tryAgain

//推荐
BuyerShow_MyStyle_Edit
Feedback_Alert_TryAgain
4. Model
5. 前缀
6. and & with
7. 缩写

| 全称 | 缩写 |
| ---------------: | :-----------------|
| UITableViewCell | Cell |
| UICollectionCell | CCell |
| UIViewController | VC |
| UINavigationController | NC |
| UIGestureRecognizer | GR |
| Allocate | alloc |
| Application | app |
| Calculate | calc |
| Deallocate | dealloc |
| Function | func |
| Horizontal | horiz |
| Information | info |
| Initialize | init |
| Integer | int |
| Maximum | max |
| Minimum | min |
| Message | msg |
| Rectangle |rect |
| Temporary |temp |
| Vertical |vert |

8. 文件命名

通用规范

1. 大括号
2. 代码长度
3. 语句中留有合适的空格
4. @Class
//不建议
@class UIView, UINavigationItem, UIBarButtonItem, UITabBarItem, UISearchDisplayController;

//推荐
@class UIView;
@class UINavigationItem, UIBarButtonItem, UITabBarItem;
@class UISearchDisplayController;
5. @property
6. 布尔值
7. 枚举
8. 分类
9. #import 的顺序
//模板
#import <系统库>
#import <第三方库>
#import "其它类"

//不建议
#import "RGModel.h"
#import <UIKit/UIKit.h>
#import <Google/Analytics.h>

//推荐
#import <UIKit/UIKit.h>
#import <Google/Analytics.h>
#import "RGModel.h"
10. If 嵌套
//不建议
if ([obj boolValue]){
    if (valid){
        //Do something
    }    
}

//推荐
if (![obj boolValue]){
    return;
}
if (!valid){
    return;
}
//Do something

代码组织

1. 生命周期
#pragma mark - Life Cycle
- (instancetype)init;
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)viewDidLayoutSubviews {}
- (void)didReceiveMemoryWarning {}
2. 协议实现
#pragma mark - NSCopying
+ (id)copyWithZone:(struct _NSZone *)zone

#pragma mark - NSObject    
- (BOOL)isEqual:(id)object;

#pragma mark - UITableViewDelegate
#pragma mark - WKNavigationDelegate
#pragma mark - YYModel
3. 事件处理
4. 私有方法
5. 公共方法
6. 属性方法
#pragma mark - Property Method
- (UILabel *)centerLabel {
if (!_centerLabel) {
    _centerLabel = [UILabel new];
}
return _centerLabel;
}

注释

1. h 文件
2. Protocol
3. 属性或变量
//推荐下面两种注释方式
/** default is NO. doesn't check superviews */
/// default is NO. doesn't check superviews
@property(nonatomic,getter=isHidden) BOOL hidden;
4. Block 和 方法

快捷键:alt + command + / 自动生成上述注释模板

5. 其它推荐注释方式
Responses