内容简介:为了增加代码的可读性和可维护性,我们今天准备拟一篇代码规范博客。本博客分两部分来说:项目结构规范 以及 代码风格规范如果觉得不错,大家也可以拿来借鉴一下。
为了增加代码的可读性和可维护性,我们今天准备拟一篇代码规范博客。
本博客分两部分来说:项目结构规范 以及 代码风格规范
如果觉得不错,大家也可以拿来借鉴一下。
一、项目结构:
整体结构:
主要模块结构:
子模块结构:
代码风格:
-
类扩展申明规范:
-
常量、静态变量申明在前
-
@property申明同一类别放在一起,不同类别换行写
-
包括空格规范注意点如下代码示例:
static NSString *mineCellId = @"mineCellId";
@interface QiMineViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray*cellDatas;
@end
-
方法编写规范:
-
前括号提至方法后
-
同一模块功能代码写在一起
-
不同模块功能换行写
- (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 (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
#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*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*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
作者:MrLiuQ
链接:https://www.jianshu.com/p/9b10331970a5
以上所述就是小编给大家介绍的《iOS 代码规范篇》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Filter Bubble
Eli Pariser / Penguin Press / 2011-5-12 / GBP 16.45
In December 2009, Google began customizing its search results for each user. Instead of giving you the most broadly popular result, Google now tries to predict what you are most likely to click on. Ac......一起来看看 《The Filter Bubble》 这本书的介绍吧!