Objective - C 的命名规范

本书作者:陈浩南
iOS开发代码规范
1.关于命名
1.1统一要求
含义清楚, 尽量做到不需要注释也能了解其作用

使用全称不使用缩写

1.2类的命名
大驼峰式命名:每一个单词的首字母都采用大写字母

例子: **HomePage****V****iewController**

前缀要求

框架相关的类: 使用ITT做前缀
  例子: **ITTImageView**
项目相关的类: 使用项目中自定义的前缀,或不使用前缀
  例子: **HPHomeViewController** 或 **HomeViewController**

后缀要求

 **V****iewController**: 使用**V****iewController**做后缀
   例子: **HomeViewController**
 View: 使用**V****iew**做后缀
   例子: **AlertView**
 **UITableCell**:使用**Cell**做后缀
   例子: **NewsCell**
 **Protocol**: 使用**Delegate**或者**DataSource**作为后缀
   例子: **UITableViewDelegate**
 **UI**控件依次类推

1.3私有变量
小驼峰式命名: 第一个单词以小写字母开始, 第二个单词的首字母大写,例如:firstNamelastName

以_开头,第一个单词首字母小写

例子: **NSString** ***_somePrivateVariable**

私有变量放在.m文件中声明

1.4 property变量
小驼峰式命名

例子: **@property** **(nonatomic, strong) NSString *userName;**

禁止使用synthesize关键词

1.5宏命名
全部大写, 单词间用_分隔。

例子: **#define THIS_IS_AN_MACRO @” THIS_IS_AN_MACRO”**

1.6 Enum
Enum类型的命名与类的命名规则一致

Enum中枚举内容的命名需要以该Enum类型名称开头

例子

** ****typedef**** ****enum** : NSUInteger{
** SampleEnumUndefined =** 0****,
** SampleEnumOne,**
** SampleEnumTwo**
** } SampleEnum;**
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进行声明
例子:
#import "CodeStandardViewController.h"
//define your private variables and methods here
@interface**** ****CodeStandardViewController ()
{
}
- (void)samplePrivateMethod;
@end

#define THIS_IS_AN_SAMPLE_MACRO @****"THIS_IS_AN_SAMPLE_MACRO"
@implementation CodeStandardViewController
#pragma mark - private methods
- (void)samplePrivateMethod
{
** //some code**
}
3关于注释
最好的代码是不需要注释的 尽量通过合理的命名
良好的代码把含义表达清楚 在必要的地方添加注释
注释需要与代码同步更新
4关于UI布局
使用Interface Builder进行界面布局
Xib文件的命名与其对应的.h文件保持相同
Xib文件中控件的组织结构要合理Xib文件中控件需要有合理的可读性强的命名,方便他人理 解
5格式化代码
5.1指针 “*****” 位置
例子: NSString userName;
5.2方法的声明和定义
- 、****+ 和返回值之间留一个空格,方法名和第一个参数之间不留空格
** - (id)initWithNibName:(NSString
* )nibNameOrNil
bundle:(****NSBundle*****)nibBundleOrNil **
{...}
5.3代码缩进
使用
xcode
*默认缩进,即 tab = 4空格

使用xcoder****e-indent功能定期对代码格式进行整理

相同类型变量声明需要独行声明

 例子:
 **CGFloat****oringX = frame.****origin****.****x****;**

** ** CGFloat oringY = frame.****origin****.****y****;
** ** CGFloat lineWidth = frame.****size****.****width****;
** MethodMethod**之间空一行

例子:
** #pragma mark - private methods**
** - (****void****)samplePrivateMethod**
** {****...****}**
** - (****void****)sampleForIf**
** {****...****}**
5.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
** {...}**

5.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****;**
** }**
** }**
任何需要写大括号的部分,不得省略

错误示例:
**- (****void****)wrongExamples**

** {**
** ****BOOL****someCondition =** YES****; **
** ****if****(someCondition)

** ****NSLog****(****@"this is wrong!!!"****);**
** ****while****(someCondition)**
** ****NSLog****(****@"this is wrong!!!"****);**
** } **
6推荐使用
该部分内容推荐大家遵守,非强制要求
** Notification Subject** 命名
建议项目中统一以SUBJECT_XXX_XXX的格式命名

例子:**SUBJECT_USER_DID_LOGIN**

** Dictionary Key**
** Dictionary中的Key**统一在一个地方定义,并使用统一前缀

如:KEY_XXX
.m件中Method布局

  •  *** lifecyle methods **分组尽量放在类文件的最上方,如:
      **- (****id****)init**
    

** - (****void****)viewDidLoad**** **
** Xcode 5**支持方法注释
例子:

其他
使用使用括号“()”来强调运算符优先级

使用空格将相同的变量、属性对齐,使用换行分组

例子:
NS_CLASS_AVAILABLE_IOS****(****2****_0) @interface UIViewController : UIResponder <****NSCoding****, UIAppearanceContainer****> {
** ** @package
** ** *UIView**** _view;
** ** *UITabBarItem**** _tabBarItem;
** ** UINavigationItem *_navigationItem;
** ** *NSArray**** _toolbarItems;
** ** *NSString**** _title;


** ** *NSString**** _nibName;
** ** *NSBundle**** _nibBundle;


** ** UIViewController *_parentViewController; // Nonretained


** ** UIViewController*****_childModalViewController;
** ** UIViewController*****_parentModalViewController; // Nonretained
** ** UIViewController*****_previousRootViewController; **// Nonretained******

你可能感兴趣的:(Objective - C 的命名规范)