编码规范
先放上一篇苹果官方的编码规范:
下面是文档中没有提到的:
一.命名规范
属性
.h/.m中属性命名,以m开头,为了方便区分系统变量和自己变量,后面驼峰:m_OneTwoThree。
@property (nonatomic, strong) HMHomeInfoModel *m_HomeInfoModel;
常量
统一使用类型常量,少用#define,好处是清楚的描述了常量的含义。
如果是私有的常量,只在.m中使用,那么在.m的import下这样定义:
static const NSTimeInterval kAnimationDuration = 0.3;
如果要对外公开某个常量,比如NSNotification相关的常量,横跨很多类使用,这样定义:
// in the header file
extern NSString *const EOCStringConstant;
// In the implementaion file
NSString *const EOCStringConstant = @"VALUE";
由于是全局变量,为了避免冲突,命名的前面部分最好是使用与之相关的类名做前缀。
二.UIViewController代码结构
代码顺序按照:life cycle --> delegate --> event --> private method --> getter & setter
# pragma mark - life cycle
// dealloc放最前面、viewDidLoad、viewWillAppear等
# pragma mark - UITableViewDataSource & UITableViewDelegate
// tableview代理
# pragma mark - CustomDelegate
// 自定义代理,代理名称要拼对,这样可以通过commond + 鼠标直接点过去查看
# pragma mark - event response
// 事件响应,如点击UIButton的事件
# pragma mark - private method
// 尽量放到category或者辅助类中写
# pragma mark - getter & setter
# pragma mark - 埋点
三.格式
函数
前花括号不换行:
void function {
}
if else
if和()间有空格,()和{ 间有空格,花括号不换行,else前后有空格,花括号不换行
if () {
} else {
}
运算符号,前后有空格,
max =(m + n) / 2;
if (a && b) {
}
四.约定
创建新模块
先在Finder里创建物理文件夹,拖拽到工程当中,再添加类文件。
五. 一些其他规定
weak & strong
使用@HMWeakify(self)和@HMStrongify(self);
六. 尽量不使用xib、storyboard,尽量使用纯代码。
主要是对于国际化,这个简直太痛苦了。