iOS开发代码规范
Posted fearlessyyp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发代码规范相关的知识,希望对你有一定的参考价值。
1、关于命名
1.1统一要求
- 含义清楚, 尽量做到不需要注释也能了解其作用,若做不到,就加注释
- 使用全称不使用缩写
1.2类的命名
- 大驼峰式命名:每一个单词的首字母都采用大写字母
例子: MFHomePageViewController - 后缀要求
- ViewController: 使用ViewController做后缀
例子: MFHomeViewController - View: 使用View做后缀
例子: MFAlertView - UITableCell:使用Cell做后缀
例子: MFNewsCell - Protocol: 使用Delegate或者DataSource作为后缀
例子: UITableViewDelegate - UI控件以此类推
- ViewController: 使用ViewController做后缀
1.3私有变量
- 小驼峰式命名: 第一个单词以小写字母开始, 第二个单词的首字母大写
例如:firstName、lastName - 以_开头,第一个单词首字母小写
例子: NSString * somePrivateVariable - 私有变量放在.m文件中声明
1.4 property变量
- 小驼峰式命名
例子: @property (nonatomic, copy) NSString *userName; - 禁止使用synthesize关键词
1.5宏命名
- 全部大写, 单词间用_分隔。[不带参数]
例子: #define THIS_IS_AN_MACRO @” THIS_IS_AN_MACRO” - 驼峰命名。[带参数]
#define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
1.6 Enum
- Enum类型的命名与类的命名规则一致
- Enum中枚举内容的命名需要以该Enum类型名称开头
例子
1 typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) { 2 AFNetworkReachabilityStatusUnknown = -1, 3 AFNetworkReachabilityStatusNotReachable = 0, 4 AFNetworkReachabilityStatusReachableViaWWAN = 1, 5 AFNetworkReachabilityStatusReachableViaWiFi = 2, 6 };
1.7 Delegate命名
- 类的实例必须为回调方法的参数之一, 如
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section - 回调方法的参数只有类自己的情况,方法名要符合实际含义, 如:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView - 以类的名字开头(回调方法存在两个以上参数的情况)以表明此方法是属于哪个类的, 如:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - 使用did和will通知Delegate已经发生的变化或将要发生的变化, 如:
-(NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
2、私有方法及变量声明
2.1声明位置
在.m文件中最上方,定义空的category进行声明
例子:
1 #import "CodeStandardViewController.h" 2 //define your private variables and methods here 3 @interface CodeStandardViewController () 4 { 5 } 6 - (void)samplePrivateMethod; 7 @end 8 9 #define THIS_IS_AN_SAMPLE_MACRO @"THIS_IS_AN_SAMPLE_MACRO" 10 @implementation CodeStandardViewController 11 #pragma mark - private methods 12 - (void)samplePrivateMethod 13 { 14 //some code 15 }
3、关于注释
最好的代码是不需要注释的,尽量通过合理的命名,良好的代码把含义表达清楚,在必要的地方添加注释。
注释需要与代码同步更新。
如果做不到命名尽量的见名知意的话,就可以适当的添加一些注释或者mark。
3.1 属性注释例子:
/// 学生
@property (nonatomic, strong) Student *student;
3.2 方法声明注释:
/**
* @brief 登录验证
*
* @param personId 用户名
* @param password 密码
* @param complete 执行完毕的block
*
* @return
*/
+ (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;
4、格式化代码
4.1指针 "*" 位置
例子: NSString *userName;
4.2方法的声明和定义
在 - 、+ 和返回值之间留一个空格,方法名和第一个参数之间不留空格
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
...
}
4.3代码缩进
- 使用xcode默认缩进,即 tab = 4空格
- 使用xcode中re-indent功能定期对代码格式进行整理
- 相同类型变量声明需要独行声明
例子:
CGFloat oringX = frame.origin.x; CGFloat oringY = frame.origin.y; CGFloat lineWidth = frame.size.width;
- Method与Method之间空一行
例子:
#pragma mark - private methods - (void)samplePrivateMethod{ ... } - (void)sampleForIf{ ... }
4.4对method进行分组
使用 #pragma mark - 方式对类的方法进行分组
例子:
#pragma mark - private methods - (void)samplePrivateMethod{ ... } - (void)sampleForIf{ ... } - (void)sampleForWhile{ ... } - (void)sampleForSwitch{ ... } - (void)wrongExamples{ ... } #pragma mark - public methods - (void)samplePublicMethodWithParam:(NSString*)sampleParam{ ... } #pragma mark - life cycle methods - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ ... } - (void)viewDidLoad{ ... } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation{ ... }
4.5大括号写法
- 对于类的method: 左括号另起一行写
例子:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; }
- 对于其他使用场景: 左括号跟在第一行后边
例子
- (void)sampleForIf { BOOL someCondition = YES; if (someCondition) { // do something here } } - (void)sampleForWhile { int i = 0; while (i < 10) { // do something here i = i + 1; } } - (void)sampleForSwitch { SampleEnum testEnum = SampleEnumTwo; switch (testEnum) { case SampleEnumUndefined:{ // do something break; } case SampleEnumOne:{ // do something break; } case SampleEnumTwo:{ // do something break; } default:{ NSLog(@"WARNING: there is an enum type not handled properly!"); break; } }
- 任何需要写大括号的部分,不得省略
以上是关于iOS开发代码规范的主要内容,如果未能解决你的问题,请参考以下文章
iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用(代码片段