iOS 代码规范篇

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

一、项目结构:

整体结构:

整体结构

主要模块结构:

主要模块结构

子模块结构:

子功能模块结构

代码风格:

  • 类扩展申明规范:
    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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • 现在正式谈谈代码规范了,代码规范是一个很大的课题,由于我本人能力的局限,不能很详细生动专业的在宏观上去讲代码怎...
    麦克学会了摇滚阅读 1,162评论 1 8
  • iOS编程规范0规范 0.1前言 为􏰀高产品代码质量,指导广大软件开发人员编写出简洁、可维护、可靠、可 测试、高效...
    iOS行者阅读 4,378评论 21 35
  • 前天,偶然在朋友圈看到一个我同级的漂亮妹子总结她的2016,学习,记者团,剧社,体育部,从她描述的那些成果来说,比...
    joeday阅读 149评论 0 0
  • 在晚上计划好明天早起跑步,吃饭,然后去学习。结果在第二天闹铃响时,关掉。一睡睡到九点。迷迷糊糊的起来,刷会手机,到...
    媛来yuan阅读 284评论 0 0
  • 我是那种有性格缺陷的人也许我们长期生活在山村的人视野和观念都比较狭隘,也有些解不开的心结,望你们做晚辈的多宽容。我...
    迟桂花阅读 251评论 0 0