iOS 代码规范篇

百家 作者:iOS开发 2018-08-30 13:39:59

点击上方“iOS开发”,选择“置顶公众号”

关键时刻,第一时间送达!


作者:MrLiuQ

链接:https://www.jianshu.com/p/9b10331970a5

iOS开发整理发布,转载请联系作者获得授权

为了增加代码的可读性和可维护性,我们今天准备拟一篇代码规范博客。

本博客分两部分来说:项目结构规范 以及代码风格规范

如果觉得不错,大家也可以拿来借鉴一下。


一、项目结构:


整体结构:



主要模块结构:



子模块结构:


代码风格:

  • 类扩展申明规范:

  1. 常量、静态变量申明在前

  2. @property申明同一类别放在一起,不同类别换行写

  3. 包括空格规范注意点如下代码示例:


static NSString *mineCellId = @"mineCellId";

@interface QiMineViewController () < UITableViewDataSourceUITableViewDelegate>

@property (nonatomicstrongUIView *headView;
@property (nonatomicstrongUITableView *tableView;

@property (nonatomiccopyNSArray *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 

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

    @interface QiMineData : NSObject

    @property (nonatomiccopyNSString *title;//!<  标题文本
    @property (nonatomiccopyNSString *detail;//!<  细节文本
    @property (nonatomicassign) 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 
    #import "QiMineData.h"

    @interface QiMineCell : UITableViewCell

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

    @end


    • QiMineCell.m


    #import "QiMineCell.h"

    @interface QiMineCell()

    @property (nonatomicstrongUIImageView *iconView;
    @property (nonatomicstrongUILabel *titleLabel;
    @property (nonatomicstrongUILabel *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.035.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, .060.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 () < UITableViewDataSourceUITableViewDelegate>

    @property (nonatomicstrongUIView *headView;
    @property (nonatomicstrongUITableView *tableView;

    @property (nonatomiccopyNSArray *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(00self.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 * 250.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 *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


    【点击成为源码大神】

    ▼点击「阅读原文」进入程序员商城

    关注公众号:拾黑(shiheibook)了解更多

    [广告]赞助链接:

    四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
    让资讯触达的更精准有趣:https://www.0xu.cn/

    公众号 关注网络尖刀微信公众号
    随时掌握互联网精彩
    赞助链接
    百度热搜榜
    排名 热点 搜索指数