命名
所有命名,必須英語規(guī)范。使用優(yōu)秀的命名,代替或減少中文注釋!!
** 類命名 **
- 1 項(xiàng)目縮寫大寫打頭
- 2 按是否可以屬于 整個(gè)項(xiàng)目 還是 某個(gè)模塊 進(jìn)行第二前綴的取舍。最后以父類或者其他結(jié)尾
- 3 主要區(qū)分,公有類,私有類 的命名區(qū)分。
// 個(gè)人中心模塊PersonCenter 有 設(shè)置界面Setting(設(shè)置界面屬于整個(gè)項(xiàng)目唯一,而且可以脫離個(gè)人中心存在)
CZSettingViewController 而不需要 CZPersonCenterSettingViewController
// 產(chǎn)品模塊 Product 有 產(chǎn)品詳情界面 Detail
CZProductDetailViewController 而不能 CZDetailViewController
** NSString 等數(shù)據(jù):直接命名英語 **
NSString *name = @"";
NSInteger age = 18;
CGFloat money = 2.5;
** NSArray / NSDictionary:使用負(fù)數(shù)形式,或者后綴Source **
注:換行!
NSArray *friends = @[@"張三",
@"李四"];
NSDictionary *famaily = @{@"father":@"爸爸",
@"mother":@"媽媽"};
NSMutableArray *tableSource = [@[@"section1",
@"section2",
@"section3"] mutableCopy];
NSMutableDictionary *incomeTableSource = [@{@"key1":@"value1",
@"key2":@"value2",
@"key3":@"value3"} mutableCopy];
** 各種控件:全部添加后綴(label 除外) **
注:禁止使用 view、label 等無意義的命名!
// 用于賦值網(wǎng)絡(luò)數(shù)據(jù)的不加后綴,用于本地顯示的加后綴。命名一般參考接口!
UILabel *nameLabel = [UILabel new]; // 不變的
UILabel *name = [UILabel new]; // 根據(jù)網(wǎng)絡(luò)獲取賦值的。
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
UITextField *passwordField = [UITextField new];
UIView *contentView = [UIView new];
UITableView *productTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
等...
** 方法 **
根據(jù)返回與方法內(nèi)容進(jìn)行命名,之間多用 with、to、of、for 等連詞連接。
命名盡量符合英語句子,達(dá)到不需要注釋。
- (NSString *)substringWithRange:(NSRange)range;
- (BOOL)isEqualToString:(NSString *)aString;
- (NSRange)rangeOfString:(NSString *)searchString;
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
+ (NSDate *)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
- (void)addChildViewController:(UIViewController *)childController;
** 宏定義 與 常量 **
2者選擇性使用。
- 常量,命名k打頭,一般加上類名為第二前綴
保證名字唯一:尤其是提供外部使用的。
static NSString * const kProductTableName = @"testTabel";
static CGFloat const kContentViewLeftPadding = 20.;
// 或者.h.m 分開
FOUNDATION_EXTERN NSString *const kProductTableName;
NSString *const kProductTableName = @"testTabel";
// 參考 系統(tǒng)命名
UIKIT_EXTERN NSString *const UIKeyboardDidChangeFrameNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;
- 宏
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.]
#define IOS9_OR_LATER ([[[UIDevice currentDevice] systemVersion] compare:@"9.0"] != NSOrderedAscending)
#define IOS9_OR_EARLIER !IOS9_OR_LATER
** 圖片 **
使用 "_" 分割,不使用駝峰,命名與類的命名相似,最后需要加使用情景
navigation_back_icon
navigation_background
login_button_normal
login_button_selected
product_detail_basic_info_icon
product_detail_remark_icon
等...
格式
** 空格 與 換行 **
- 1 屬性
@property (nonatomic, copy) NSString *name;// 4個(gè) 空!
- 2 賦值,計(jì)算,比較
self.title = @"首頁";// 2個(gè) 空!
- 3 方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// do (內(nèi)容上下不空行) 注意上面 3個(gè) 空!
}
- 4 if - else
if (a == 0) {
// do 上下 空一行
} else if (a == 1) {
// do 上下 空一行
} else {
// do 上 空一行,下 不空
}
注:行數(shù)小于3行,考慮美觀,上下都不空行,注意空格!
- 5 方法之間 空一行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return cell;
}
- 6 方法內(nèi)部 小模塊 之間 空一行
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.segmentView];
[self.view addSubview:self.contentView];
[self.segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view).mas_offset(64);
make.left.and.right.mas_equalTo(self.view);
make.height.mas_equalTo(50);
}];
self.viewControllers = [NSMutableArray array];
self.viewControllers = [@[self.detailViewController,
self.performanceViewController,
self.relatedDocumentViewController,
self.similarProductViewController] mutableCopy];
}
- 7 所有多參數(shù)方法冒號對齊
- (void)loginWithEmployeeNo:(NSString *)employeeNo
orgCode:(NSString *)orgCode
loginPassword:(NSString *)loginPassword
success:(ZJTClientManagerBlock)success
failure:(ZJTClientManagerBlock)failure {
}
類組織規(guī)范
- 1每個(gè)類 不超過500,最好少于300(除 控件初始化 外布局代碼,代碼量會(huì)多一些,storyboard的就少)
#pragma mark - View lifeCycle
#pragma mark - Methods
#pragma mark - TableView Delegate
#pragma mark - Other Delegate
#pragma mark - Layout Views (處理UI布局,一般storyboard 不需要,代碼布局會(huì)有大量,對應(yīng)下面 Getter 也會(huì)大量)
#pragma mark - Getter Setter
- 2 引入頭文件,按照 系統(tǒng)框架,其他SDK,ViewController,View,Model 順序添加。
#import "CZHomeViewController.h"
#import "CZAPPHeader.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import "CZAViewController.h"
#import "CZBViewController.h"
#import "CZAView.h"
#import "CZBCell.h"
#import "CZAModel.h"
#import "CZBModel.h"
- 3 接口 與屬性:盡量,所有實(shí)例,都使用屬性,并且使用懶加載方式初始化控件。
@interface CZHomeViewController ()
空!
@property (nonatomic, strong) UITableView *orderTableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
空!
@end
- 4 整理:在對View 傳入ViewModel 后,對數(shù)據(jù)進(jìn)行處理與賦值時(shí),統(tǒng)一先處理數(shù)據(jù),在統(tǒng)一賦值,分成2個(gè)小模塊
-(void)bindViewModel:(id)model {
// 可能model 返回的數(shù)據(jù)不能直接顯示,所以先處理
// 然后統(tǒng)一賦值
}
// 其他地方類似,以免數(shù)據(jù)過于混亂
- 5 待處理:在存在疑問或者未完成處,打warning (自己名字縮寫如cz)然后描述內(nèi)容,中英文即可(英文可以在warning區(qū)域直接看到)
#warning
其他語法
- 點(diǎn)語法 與 []:屬性-點(diǎn)語法。方法-[]
view.backgroundColor = [UIColor orangeColor];
[UIApplication sharedApplication].delegate;
- 多層嵌套:嵌套三層即以上,就應(yīng)該考慮提出,按照上面集中處理,集中賦值的原則,應(yīng)該合理應(yīng)用嵌套。
// 多層嵌套
imageView.image = [[UIImage imageNamed:[self.dataSource objectAtIndex:2]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// 拆分
UIImage *currentAdImage = [UIImage imageNamed:[self.dataSource objectAtIndex:2]];
imageView.image = [currentAdImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];