在我們開始編碼之前,其實(shí)我想每一個(gè)人都有其自己的習(xí)慣,也就是自己編碼習(xí)慣 ,然而我們的自己習(xí)慣都是對(duì)的嗎?或者說你看的習(xí)慣其他人的代碼風(fēng)格嗎? 有一些潔癖或者說處女座風(fēng)格的你我,都是會(huì)有所追求的。
先一個(gè) Test 類 開始...
#import "PQTestViewController.h"
#import <Masonry.h>
#define PQ_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define PQ_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
static const CGFloat kTestTableViewCellHeight = 50.0f;
static NSString *const kTestTableViewCellIden = @"kTestTableViewCellIden";
@interface PQTestViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation PQTestViewController
#pragma mark - Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self pqInitView];
[self pqLayoutView];
[self requestData];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//:To Do
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//:To Do
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//:To Do
}
- (void)dealloc {
NSLog(@"TestVC Dealloc");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Method
- (void)requestData {
// Get data
}
#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTestTableViewCellIden forIndexPath:indexPath];
// cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
#pragma mark - InitAndLayout
- (void)pqInitView {
[self.view addSubview:self.tableView];
}
- (void)pqLayoutView {
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsZero);
}];
}
#pragma mark - Getter
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.rowHeight = kTestTableViewCellHeight;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTestTableViewCellIden];
}
return _tableView;
}
- (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
@end
#import <UIKit/UIKit.h>
//用戶性別
typedef NS_ENUM(NSUInteger, TestUserEnumSexType){
TestUserEnumSexTypeSecret = 0, // 0 保密
TestUserEnumSexTypeMale, // 1 男
TestUserEnumSexTypeFemale // 2 女
};
@interface PQTestViewController : UIViewController
/**
用戶性別選擇
*/
@property (nonatomic, assign) TestUserEnumSexType userSexType;
@end
對(duì)于具體的代碼規(guī)范,我是推薦:
-
禪與 Objective-C 編程藝術(shù) (Zen and the Art of the Objective-C Craftsmanship 中文翻譯)
這其中基本把我們平常用到的都提到了,而且我是很認(rèn)同。 -
編寫高質(zhì)量 iOS 與 OSX 代碼的52個(gè)有效方法
配合著里面闡述的方法,我覺的基本 OK 啦。
而我們組也總結(jié)了下 GB 代碼規(guī)范(此處是可持續(xù)更新的),雖說有點(diǎn)亂,但是還是還很齊的。
另外我再補(bǔ)充兩個(gè)建議點(diǎn):
空
就是類似下面這種空行:
// 建議
@interface PQTestViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
// 不建議
@interface PQTestViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
當(dāng)然方法之間肯定是要空行的,同時(shí)如屬性單詞之間的空格,和方法名前的空格,也需要注意。
// 建議
@property(nonatomic,strong)UITableView*tableView;
-(void)requestData
// 不建議
@property (nonatomic, strong) UITableView *tableView;
- (void)requestData
順序
#pragma mark - Life Cycle
#pragma mark - Method
#pragma mark - Delegate
#pragma mark - InitAndLayout
#pragma mark - Getter
個(gè)人習(xí)慣在么一個(gè)類中,按模塊分區(qū)域,最上面是 聲明周期,下面是方法,緊接著是代理,然后再是布局,最后是懶加載,這樣感覺每一個(gè)類中,不需要通過搜索,直接很快就可以下意思的找到我們想找的地方。
另外同時(shí)例如在生命周期這個(gè)模塊中,我也是推薦按順序走的
// 建議
- (void)viewDidLoad
- (void)viewWillAppear:(BOOL)animated
- (void)viewDidAppear:(BOOL)animated
- (void)viewWillDisappear:(BOOL)animated
- (void)dealloc
// 不建議
- (void)viewWillAppear:(BOOL)animated
- (void)dealloc
- (void)viewDidAppear:(BOOL)animated
- (void)viewWillDisappear:(BOOL)animated
- (void)viewDidLoad
假如這個(gè)順序亂了,我都感覺怪怪的,畢竟其生命周期是這樣的嘛...
這樣一下來,相對(duì)來說每一個(gè)類都會(huì)更清晰,感覺會(huì)更舒服,也利于后期的開發(fā)和維護(hù)的。
當(dāng)然,每一個(gè)團(tuán)隊(duì)或多或少都有自己的規(guī)范,不同之處都可理解,但是規(guī)范一定還是要有的。