iOS 代码规范篇

2,365 阅读2分钟

为了增加代码的可读性和可维护性,我们今天准备拟一篇代码规范博客。
本博客分两部分来说:项目结构规范 以及 代码风格规范。
同时,这也是我们QiShare团队的代码规范~
如果觉得不错,大家也可以拿来借鉴一下。

一、项目结构:

整体结构:

整体结构

主要模块结构:

主要模块结构

子模块结构:

子功能模块结构

代码风格:

  • 类扩展申明规范: 1.常量、静态变量声明在前 2.@property声明同一类别放在一起,不同类别换行写 3.包括空格规范,几个注意点如下代码示例:
static NSString *mineCellId = @"mineCellId";

@interface QiMineViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSArray<QiMineData *> *cellDatas;

@end
  • 方法编写规范:
  1. 前括号提至方法后
  2. 同一模块功能代码写在一起
  3. 不同模块功能换行写
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupHeadView];
    [self setupTableView];
    
    [self getCellDatas];
}
  • 方法归类:
#pragma mark - Private Functions

//code...
//上空一行
//下空两行


#pragma mark - Action functions

//code...
//上空一行
//下空两行


#pragma mark - Request functions

//code...
//上空一行
//下空两行


#pragma mark - xxxDataSource

//code...
//上空一行
//下空两行


#pragma mark - xxxDelegate

//code...
//上空一行
//下空两行



以下是我写的一个简单的MVC结构 代码示例:

Model层:

  • QiMineData.h
#import <Foundation/Foundation.h>

typedef NS_ENUM(NSUInteger, QiMineDataType) {
    QiMineDataTypeDefault,//!< 默认类型
    QiMineDataTypeOne,//!< 类型1
    QiMineDataTypeTwo,//!< 类型2
};

@interface QiMineData : NSObject

@property (nonatomic, copy) NSString *title;//!< 标题文本
@property (nonatomic, copy) NSString *detail;//!< 细节文本
@property (nonatomic, assign) QiMineDataType type;//!< 数据类型

/**
 @brief QiMineData初始化方法
 @param dic 初始化数据源字典
 @return QiMineData实例
 */
- (instancetype)initWithDic:(NSDictionary *)dic;

@end
  • QiMineData.m
#import "QiMineData.h"

@implementation QiMineData

- (instancetype)initWithDic:(NSDictionary *)dic {
    
    self = [super init];
    
    if (self) {
        
        _title = dic[@"title"];
        _detail = dic[@"detail"];
    }
    
    return self;
}

@end

View层:

  • QiMineCell.h
#import <UIKit/UIKit.h>
#import "QiMineData.h"

@interface QiMineCell : UITableViewCell

@property (nonatomic, strong) QiMineData *cellData;//!< cell的数据model实例

@end
  • QiMineCell.m
#import "QiMineCell.h"

@interface QiMineCell()

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *detailLabel;

@end

@implementation QiMineCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        _iconView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_iconView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.font = [UIFont systemFontOfSize:16.0];
        _titleLabel.textColor = [UIColor blackColor];
        [self.contentView addSubview:_titleLabel];
        
        _detailLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _detailLabel.font = [UIFont systemFontOfSize:14.0];
        _detailLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_detailLabel];
    }
    
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat margin = 10.0;
    CGFloat padding = 10.0;
    CGFloat MaxHeight = self.contentView.frame.size.height;
    CGFloat MaxWidth = self.contentView.frame.size.width;
    
    _iconView.frame = CGRectMake(margin, margin, 35.0, 35.0);
    _iconView.layer.cornerRadius = _iconView.frame.size.width / 2;
    _iconView.layer.masksToBounds = YES;
    
    _titleLabel.frame = CGRectMake(_iconView.frame.origin.x + _iconView.frame.size.width + padding, .0, 60.0, MaxHeight);
    
    _detailLabel.frame = CGRectMake(_titleLabel.frame.origin.x + _titleLabel.frame.size.width + padding, MaxHeight * 0.5, MaxWidth - _titleLabel.frame.size.width - padding * 2 - margin *2, MaxHeight * 0.5);
}

-(void)setCellData:(QiMineData *)cellData {
    
    _cellData = cellData;
    
    _iconView.image = [UIImage imageNamed:@"qishare"];
    _titleLabel.text = cellData.title;
    _detailLabel.text = cellData.detail;
}

Controller层

  • QiMineViewController.h
#import "QiBaseViewController.h"

@interface QiMineViewController : QiBaseViewController

@end
  • QiMineViewController.m
#import "QiMineViewController.h"
#import "QiMineCell.h"

static NSString * const mineCellId = @"mineCellId";

@interface QiMineViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, copy) NSArray<QiMineData *> *cellDatas;

@end

@implementation QiMineViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupHeadView];
    [self setupTableView];
    
    [self getCellDatas];
}


#pragma mark - Private Functions

- (void)setupHeadView {
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    _headView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_headView];
}

- (void)setupTableView {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _headView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
    _tableView.estimatedSectionHeaderHeight = .0;
    _tableView.estimatedSectionFooterHeight = .0;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = self.tableFooterView;
    [_tableView registerClass:[QiMineCell class] forCellReuseIdentifier:mineCellId];
    [self.view addSubview:_tableView];
}

- (UIView *)tableFooterView {
    
    UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(.0, .0, _tableView.frame.size.width, 150.0)];
    
    CGFloat horMargin = 50.0;
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    logoutButton.frame = CGRectMake(.0, .0, tableFooterView.frame.size.width - horMargin * 2, 50.0);
    logoutButton.center = tableFooterView.center;
    [logoutButton setTitle:@"这是一个Button" forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(logoutButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    logoutButton.layer.borderColor = [UIColor purpleColor].CGColor;
    logoutButton.layer.cornerRadius = logoutButton.frame.size.height / 2;
    logoutButton.layer.borderWidth = 1.0;
    logoutButton.layer.masksToBounds = YES;
    [tableFooterView addSubview:logoutButton];
    
    return tableFooterView;
}


#pragma mark - Action functions

- (void)logoutButtonClicked:(id)sender {
    
    NSLog(@"ButtonClick");
}


#pragma mark - Request functions

- (NSArray *)getCellDatas {//这里模仿网络请求成功
    
    QiMineData *data = [[QiMineData alloc] initWithDic:@{@"title": @"QiShare", @"detail": @"Hello,everyone!"}];
    
    NSMutableArray<QiMineData *> *datas = [NSMutableArray array];
    for (int i = 0;  i < 20 ; i++) {
        [datas addObject:data];
    }
    
    _cellDatas = datas;
    
    return _cellDatas;
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _cellDatas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    QiMineCell *cell = [tableView dequeueReusableCellWithIdentifier:mineCellId forIndexPath:indexPath];
    cell.cellData = _cellDatas[indexPath.row];
    
    return cell;
}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 50.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 55.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"QiShare提醒您,您点击了%ld个cell",(long)indexPath.row);
}

@end