命名规范
-
类名大驼峰,前边加HP防止重名
-
变量名和方法名
-
遵守小驼峰原则
-
返回性的方法避免使用get开头,可以以内容名称开头
-
执行性的方法以动词开头
-
方法尽量含义清晰,见文达意
-
-
避免各种神秘数字. 应使用枚举或者宏,而且枚举要以OC的方式命名枚举,尽量避免C方式,如下:
-
// 推荐 普通枚举
typedefNS_ENUM(NSUInteger,LrdOutputViewDirection){
kGHDropDownViewDirectionLeft=1,
kGHDropDownViewDirectionRight
};
// 推荐 位运算枚举
typedefNS_OPTIONS(NSUInteger,EnumValue){
EnumValueA=1<<0,
EnumValueB=1<<1,
EnumValueC=1<<2,
};
// 不推荐
typedefenum{
GHArcLabelDelBtnType=1<<1,
GHArcLabelArcType=1<<2,
GHArcLabelDelLineType=1<<3
}GHArcLabelType;
// 推荐 使用枚举或常量或宏定义代替数字
if(![[UIApplicationsharedApplication].keyWindowviewWithTag:kShowFeedBackView]){
[HPFiveStarViewshowFiveStarViewWithFeedback:^{
[weakSelfshowFeedback];
}];
}
// 不推荐
if(![[UIApplicationsharedApplication].keyWindowviewWithTag:88885]){
[HPFiveStarViewshowFiveStarViewWithFeedback:^{
[weakSelfshowFeedback];
}];
}
-
尽量向Cocoa接口学习,但是要避免与Cocoa接口命名冲突,苹果代码规范
-
避免使用拼音命名
代码格式
-
尽量保持整个工程的代码格式一致
-
一个文件内的代码必须格式一致,尽量避免出现以下两种风格
-
// 推荐 大括弧放在上方
-(void)viewDidLoad{
[superviewDidLoad];
}
// 大括弧不要放在下方
-(void)viewDidLoad
{
[superviewDidLoad];
}
// 数组与字典初始化,注意换行。
self.titleArray=@[@[@"花粉儿小金库"],
@[@"芝麻信用"],
@[
@"我发布的",
@"我卖出的",
@"我买到的",
@"我的退款"
],
@[@"我收藏的"],
@[
@"帮助中心",
@"设置"
]];
// 不推荐
self.titleArray=@[@[@"花粉儿小金库"],@[@"芝麻信用"],@[@"我发布的",@"我卖出的",@"我买到的",@"我的退款"],@[@"我收藏的"],@[@"帮助中心",@"设置"]];
-
注释格式
-
类注释 sdfasfd
-
//
// HPMineNewViewController.h
// GHZeusLibraries
//
// 新版 我的页面 2.7.0
//
// Created by Zhang Weifan on 2017/7/19.
// Copyright © 2017年 张冠华. All rights reserved.
//
#import "GHMainViewController.h"
-
方法注释,推荐在函数或变量上方使用快捷键:cmd + option + /
-
/**
根据运送类型、运费返回需要显示的不同字符串
@param shipType 运送类型 1包邮 2不包邮 3当面交易
@param postage 运费
@return 显示的不同字符串
*/
+(NSString*)emsStringWithShipType:(NSInteger)shipTypepostage:(NSInteger)postage;
成员变量注释,同样使用快捷键:cmd + option + /
ps: 注释字数多时使用快捷键的方式,注释少时可以放在后方。
/**
商品支付, 支付宝支付啊 微信支付啥的 1微信 2支付宝 3 退换货 4包邮
*/
@property(nonatomic,strong)NSArray*goodsOperations;
@property(nonatomic,assign)NSIntegergoodsPV; //浏览量
@property(nonatomic,assign)NSIntegergoodsState; //状态
@property(nonatomic,strong)NSArray*goodsTypes; //商品类型, 全新之类的
@property(nonatomic,assign)BOOLisLike; //喜欢
书写格式
-
运算符之间加空格
-
逗号后加空格
-
while, ifelse, swich, for, forin 系统写法, if 不能简便写法
-
swich case 大括弧写在下面
-
-
一段逻辑之间加换行
-
函数内部代码尽量不能超过 70 行
-
声明/实现/调用时,函数参数 >= 3 时加换行
-
宏定义使用大驼峰写法
头文件与实现文件排版
头文件 .h
-
尽量不要暴露 .h 文件,避免引用 .h 文件。使用 @class @protocol 代替。
-
尽量不要暴露私有成员变量。(除非继承)
-
静态方法在成员方法之前。
@classReferenceClass;
@protocolReferenceDelegate;
@interfaceMyClass: NSObject
@property(nonatomic,weak)id<ReferenceDelegate>delegate;
@property(nonatomic,strong)ReferenceClass*model;
+(void)funcWithModel:(ReferenceClass*)model;
-(void)func;】
@end
实现文件 .m
-
import 文件顺序
-
自身头文件
-
protocol
-
view
-
controller
-
model
-
网络请求
-
-
interface
-
delegate数量 >= 3 则换行
-
-
property 顺序
-
delegate
-
view
-
controller
-
model
-
属性
-
网络请求
-
-
函数列表顺序
-
系统方法
-
static func
-
clicke event
-
delegate
-
private func
-
configSubview
-
getter & setter
-
-
#import "MyClass.h"
//view
#import "MyView1.h"
#import "MyView2.h"
//controller
#import "ViewController1.h"
#import "ViewController2.h"
//model
#import "ReferenceClass.h"
#import "ReferenceClass2.h"
@interfaceMyClass()<UITableViewDataSource,
UITableViewDelegate,
UITableViewDelegate1,
UITableViewDelegate2,
UISearchBarDelegate>
//view
@property(nonatomic,weak)UITableView*tableView;
@property(nonatomic,weak)HPCashboxHeaderView*headerView;
@property(nonatomic,weak)UISearchBar*searchBar;
@property(nonatomic,strong)ATDatePickerView*datePickView;
@property(nonatomic,weak)HPNavigationView*navigationBarView;
@property(nonatomic,weak)UIButton*searchCancelBtn;
//搜索页面控件
@property(nonatomic,weak)HPDateHeaderView*dateHeaderView;
为了解决从隐藏 nav 进入不隐藏+透明 nav 的页面时闪烁的问题
//@property (nonatomic, weak) HPCashboxHeaderView *tmpHeaderView;
//model
/*
页面展示数据, key : value 数组. 每一个 dict 中只有一个 key:value 对.
key: 时间.
value: NSArray<HPTransactionDetail *>数组
*/
@property(nonatomic,strong)NSArray<NSDictionary*>*transactions;
@property(nonatomic,strong)HPCashboxModel*cashboxModel;
@property(nonatomic)NSString*dateStr;
@end
@implementationMyClass
-(void)viewDidLoad{
[superviewDidLoad];
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
}
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
[superviewWillDisappear:animated];
}
#pragma mark - Static func
+(void)funcWithModel:(ReferenceClass*)model{
}
#pragma mark - clicked event
#pragma mark - delegate
#pragma mark - private func
#pragma mark - configSubview
#pragma mark - getter & setter
目录说明
-
HPTools 大部分模块都会使用到的
-
group 命名以 NSString + external 的形式, 所有工具分类都依照此格式。
-
功能控件以功能命名
-
-
HPService 与大部分业务逻辑相关的中间层
-
外增加Service层处理业务逻辑,比如Order目录下有:Model,View,Controller,Service四个目录
-
-
GHUtilsMacros 新的全局宏定义放到该文件内部
-
HPManger 业务功能模块的封装
-
ResourceManagement 移动到 Utils
-
将对应功能 manger 移动到相应功能模块下
-
-
HPViews 通用 View 封装
-
HPModels 待定
-
模块名
-
views
-
models
-
viewControllers
-
tools 该功能模块会使用到
-
manangers 业务功能封装
-
XCode Key Bindings
Command | Default Key | Better Key | Description |
Open Quickly… | cmd-shift-o | 快速跳转文件 | |
Jump to Definition | cmd-ctrl-j | 跳转到定义 | |
Find Selected Symbol in Workspace | - | cmd-ctrl-s | 因查找符号表,比查找文本更精准 |
Show Document Items | - | cmd-j | |
Reveal in Project Navigator | cmd-shift-j | ||
Jump to Next/Previous Conunterpart | cmd-ctrl-⇅ | 在 .h 和 .m 间切换 | |
Go Forward/Back | cmd-ctrl-⇄ | 因为 MacOS 有 |