iOS 代码规范 && 效率

My App 之 代码整洁

2017-01-24  本文已影响235人  天空中的球

在我们开始编码之前,其实我想每一个人都有其自己的习惯,也就是自己编码习惯 ,然而我们的自己习惯都是对的吗?或者说你看的习惯其他人的代码风格吗? 有一些洁癖或者说处女座风格的你我,都是会有所追求的。

先一个 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

对于具体的代码规范,我是推荐:

而我们组也总结了下 GB 代码规范(此处是可持续更新的),虽说有点乱,但是还是还很齐的。
另外我再补充两个建议点:

就是类似下面这种空行:

// 建议
@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

当然方法之间肯定是要空行的,同时如属性单词之间的空格,和方法名前的空格,也需要注意。

// 建议
@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

个人习惯在么一个类中,按模块分区域,最上面是 声明周期,下面是方法,紧接着是代理,然后再是布局,最后是懒加载,这样感觉每一个类中,不需要通过搜索,直接很快就可以下意思的找到我们想找的地方。

另外同时例如在生命周期这个模块中,我也是推荐按顺序走的

// 建议
- (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 

假如这个顺序乱了,我都感觉怪怪的,毕竟其生命周期是这样的嘛...

这样一下来,相对来说每一个类都会更清晰,感觉会更舒服,也利于后期的开发和维护的。
当然,每一个团队或多或少都有自己的规范,不同之处都可理解,但是规范一定还是要有的。

上一篇下一篇

猜你喜欢

热点阅读