代碼規(guī)范的重要性
- 促進團隊合作,提高代碼的可讀性;
- 有助于 Code Review ;
- 可以降低 Bug 出現(xiàn)的概率;
Model命名
響應(yīng)
- RespMetaModel
請求
- ReqBaseModel
組件命名
eg:QYTMapHelper
包括但不限于:
- Helper
- Manager
- Meter
- View (同樣還有Picker Browser Sheet Player AlertView Menu等)
- Uploader
- Kit
- Editor
- SDK
- Logger
- Mediator
- ···
ViewController命名
eg:QYTBaseViewController
組織
QuanYanTech
簽名
Created by MaxWellPro on 16/11/14.
代碼規(guī)范
- 代碼塊
建議多使用代碼塊,可以增加代碼的閱讀性。
/**
* 初始化根視圖
*/
[self initWindow];
/**
* 初始化請求框架設(shè)置
*/
[self initNetWorkManager];
/**
* 初始化圖片緩存
*/
[self initSDWebImageManager];
- import VS include
使用 #import 引入Ojbective-C和Ojbective-C++頭文件,使用 #include 引入C和C++頭
- 指針“* =”號的位置
UIImageView *imageView = [[UIImageView alloc] init];
- 變量
NSString *_varName;
- 宏定義
// app 信息
#define APP_VERSION [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"]
#define APP_BUILD_VERSION [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleVersion"]
#define APP_DISPLAY_NAME [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleDisplayName"]
// 系統(tǒng)控件默認高度
#define StatusBarHeight (20.f)
#define TopBarHeight (44.f)
#define BottomBarHeight (49.f)
#define CellDefaultHeight (44.f)
#define EnglishKeyboardHeight (216.f)
#define ChineseKeyboardHeight (252.f)
// 加載圖片
#define PNGIMAGE(NAME) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:@"png"]]
#define JPGIMAGE(NAME) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:@"jpg"]]
#define IMAGE(NAME, EXT) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:(EXT)]]
// 顏色(RGB)
#define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBACOLOR(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
// 隨機顏色
#define RANDOM_UICOLOR [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1]
// View 圓角和加邊框
#define ViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
// View 圓角
#define ViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES]
- 常量
static NSString * const kURLProtocolHandledKey = @"BF_URLProtocolHandledKey";
- 屬性
@property (nonatomic, copy) NSString *aString;
- 成員變量使用 @private。如:
@interface MyClass : NSObject {
@private
id _myInstanceVariable;
}
- 在 - OR + 和返回值之間留1個空格,方法名和第一個參數(shù)間不留空格,并且{接在方法后邊需要留一個空格。如:
- (void)doSomethingWithString:(NSString *)theString {
...
}
- 當參數(shù)過長時,每個參數(shù)占用一行,以冒號對齊。如:
- (void)doSomethingWith:(GTMFoo *)theFoo
rect:(NSRect)theRect
interval:(float)theInterval {
...
}
- dealloc
- (void)dealloc {
NSLog(@"- [%@ dealloc]",[self class]);
}
- 待辦事項
TODO:// or FIXME://
- 換行
盡量不要出現(xiàn)2行以上的換行,合理換行。
self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
[self.tableView setDataSource:self];
[self.tableView setDelegate:self];
[self.tableView setShowsVerticalScrollIndicator:NO];
[self.tableView setShowsHorizontalScrollIndicator:NO];
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@0);
make.left.equalTo(@0);
make.bottom.equalTo(@-49);
make.right.equalTo(@0);
}];
MJWeakSelf
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[weakSelf getDataWithHeaderRefresh];
}];
- 方法的調(diào)用
調(diào)用方法沿用聲明方法的習(xí)慣。例外:如果給定源文件已經(jīng)遵從某種習(xí)慣,繼續(xù)遵從那種習(xí)慣。
所有參數(shù)應(yīng)在同一行中,或者每個參數(shù)占用一行且使用冒號對齊。如:
[myObject doFooWith:arg1 name:arg2 error:arg3];
或
[myObject doFooWith:arg1
name:arg2
error:arg3];
- mrak快速查找代碼
#pragma mark – Life Cycle
#pragma mark - Events
#pragma mark – Private Methods
#pragma mark - UITextFieldDelegate
#pragma mark - UITableViewDataSource
#pragma mark - UITableViewDelegate
#pragma mark - Custom Delegates
#pragma mark – Getters and Setters
- 懶加載
什么時候用,什么時候初始化,更智能更節(jié)能
- (UILabel *)label {
if (!_label) {
_label = [[UILabel alloc] init];
_label.text = @"1234";
_label.font = [UIFont systemFontOfSize:12];
... ...
}
return _label;
}
命名
- 類名(及其category name 和 protocal name)的首字母大寫,寫使用首字母大寫的形式
- 分割單詞
- 在面向多應(yīng)用的代碼中,推薦使用前綴。如:QYTSendMessage