1. 關(guān)于命名
1.1 統(tǒng)一要求
含義清楚航背,盡量做到不需要注釋也能了解其作用衡载,若做不到爱榕,就加注釋
使用全稱(chēng)弃舒,不適用縮寫(xiě)
1.2 類(lèi)的命名
大駝峰式命名:每個(gè)單詞的首字母都采用大寫(xiě)字母
1例子:MFHomePageViewController
后綴要求
a.ViewController: 使用ViewController做后綴
1例子:?MFHomeViewController
b.View: 使用View做后綴
1例子:?MFAlertView
c.UITableCell:使用Cell做后綴
1例子:?MFNewsCell
d.Protocol: 使用Delegate或者DataSource作為后綴
1例子:?UITableViewDelegate
1.3 私有變量
小駝峰式命名:第一個(gè)單詞以小寫(xiě)字母開(kāi)始臊诊,后面的單詞的首字母全部大寫(xiě)
1例子:firstName鸽粉、lastName
以 _ 開(kāi)頭,第一個(gè)單詞首字母小寫(xiě)
1例子:NSString?*?_somePrivateVariable
私有變量放在 .m 文件中聲明
1.4 property變量
小駝峰式命名
例子:///注釋
@property?(nonatomic,?copy)?NSString?*userName;
禁止使用synthesize關(guān)鍵詞
1.5 宏命名
全部大寫(xiě)抓艳,單詞間用 _ 分隔触机。[不帶參數(shù)]
1例子:?#define?THIS_IS_AN_MACRO?@"THIS_IS_AN_MACRO"
以字母 k 開(kāi)頭,后面遵循大駝峰命名玷或。[不帶參數(shù)]
例子:#define?kWidth?self.frame.size.width
小駝峰命名儡首。[帶參數(shù)]
1#define?getImageUrl(url)?[NSURL?URLWithString:[NSString?stringWithFormat:@"%@%@",kBaseUrl,url]]
1.6 Enum
Enum類(lèi)型的命名與類(lèi)的命名規(guī)則一致
Enum中枚舉內(nèi)容的命名需要以該Enum類(lèi)型名稱(chēng)開(kāi)頭
例子:
typedef?NS_ENUM(NSInteger,?AFNetworkReachabilityStatus)?{
AFNetworkReachabilityStatusUnknown?=?-1,
AFNetworkReachabilityStatusNotReachable?=0,
AFNetworkReachabilityStatusReachableViaWWAN?=1,
AFNetworkReachabilityStatusReachableViaWiFi?=2
};
1.7 Delegate命名
類(lèi)的實(shí)例必須為回調(diào)方法的參數(shù)之一
1例子:-(NSInteger)tableView:(UITableView*)tableView?numberOfRowsInSection:(NSInteger)section
回調(diào)方法的參數(shù)只有類(lèi)自己的情況,方法名要符合實(shí)際含義
例子:
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
以類(lèi)的名字開(kāi)頭(回調(diào)方法存在兩個(gè)以上參數(shù)的情況)以表明此方法是屬于哪個(gè)類(lèi)的
1例子:-(UITableViewCell*)tableView:(UITableView*)tableView?cellForRowAtIndexPath:(NSIndexPath?*)indexPath
使用did和will通知Delegate已經(jīng)發(fā)生的變化或?qū)⒁l(fā)生的變化
例子:
a.-(NSIndexPath*)tableView:(UITableView*)tableView?willSelectRowAtIndexPath:(NSIndexPath*)indexPath;
b.-(void)tableView:(UITableView*)tableView?didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
2. 私有方法及變量聲明
2.1 聲明位置
在.m文件中最上方偏友,定義空的category進(jìn)行聲明
例子:
#import"CodeStandardViewController.h"
//?在這個(gè)category(類(lèi)目)中定義變量和方法
@interfaceCodeStandardViewController?(){
//?聲明私有變量
}
//?私有方法
-?(void)samplePrivateMethod;
@end
@implementation?CodeStandardViewController
//?私有方法的實(shí)現(xiàn)
-?(void)samplePrivateMethod
{
//some?code
}
3.關(guān)于注釋
最好的代碼是不需要注釋的 盡量通過(guò)合理的命名
良好的代碼把含義表達(dá)清楚 在必要的地方添加注釋
注釋需要與代碼同步更新
如果做不到命名盡量的見(jiàn)名知意的話(huà)蔬胯,就可以適當(dāng)?shù)奶砑右恍┳⑨尰蛘適ark
3.1 屬性注釋
例子:
///?學(xué)生
@property?(nonatomic,?strong)?Student?*student;
3.2 方法聲明注釋
/**
*?@brief?登錄驗(yàn)證
*
*?@param?personId?用戶(hù)名
*?@param?password?密碼
*?@param?complete?執(zhí)行完畢的block
*
*?@return
*/
+?(void)loginWithPersonId:(NSString?*)personId?password:(NSString?*)password?complete:(void(^)(CheckLogon?*result))complete;
4.關(guān)于UI布局
使用Interface Builder進(jìn)行界面布局
Xib文件的命名與其對(duì)應(yīng)的.h文件保持相同
Xib文件中控件的組織結(jié)構(gòu)要合理,Xib文件中控件需要有合理的可讀性強(qiáng)的命名位他,方便他人理解
5.格式化代碼
5.1 指針 "*" 位置
定義一個(gè)對(duì)象時(shí)氛濒,指針 "*" 靠近變量
1例子:?NSString?*userName;
5.2 方法的聲明和定義
在- 产场、+和返回值之間留一個(gè)空格,方法名和第一個(gè)參數(shù)之間不留空格
1-?(id)initWithNibName:(NSString?*)nibNameOrNilbundle:(NSBundle?*)nibBundleOrNil{...}
5.3 代碼縮進(jìn)
使用 xcode 默認(rèn)縮進(jìn)泼橘,即 tab = 4空格
使用 xcode 中 re-indent 功能定期對(duì)代碼格式進(jìn)行整理
相同類(lèi)型變量聲明需要獨(dú)行聲明
例子:
CGFloatoringX?=?frame.origin.x;
CGFloatoringY?=?frame.origin.y;
CGFloatlineWidth?=?frame.size.width;
Method與Method之間空一行
例子:
#pragma?mark?-privatemethods
-?(void)samplePrivateMethod
{...}
-?(void)sampleForIf
{...}
5.4 對(duì)method進(jìn)行分組
使用 #pragma mark - 方式對(duì)類(lèi)的方法進(jìn)行分組
例子:
#pragma?mark?-privatemethods
-?(void)samplePrivateMethod
{...}
-?(void)sampleForIf
{...}
-?(void)sampleForWhile
{...}
-?(void)sampleForSwitch
{...}
-?(void)wrongExamples
{...}
#pragma?mark?-publicmethods
-?(void)samplePublicMethodWithParam:(NSString*)sampleParam
{...}
#pragma?mark?-?life?cycle?methods
-?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil
{...}
-?(void)viewDidLoad
{...}
-?(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{...}
5.5 大括號(hào)寫(xiě)法
對(duì)于類(lèi)的method: 左括號(hào)另起一行寫(xiě)(遵循蘋(píng)果官方文檔)
例子:
-?(id)initWithNibName:(NSString?*)nibNameOrNilbundle:(NSBundle?*)nibBundleOrNil
{
self?=?[superinitWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if(self)?{
//?Custom?initialization
}
returnself;
}
對(duì)于其他使用場(chǎng)景: 左括號(hào)跟在第一行后邊
例子:
-?(void)sampleForIf
{
BOOL?someCondition?=?YES;
if(someCondition)?{
//?do?something?here
}
}
-?(void)sampleForWhile
{
inti?=0;
while(i?<10)?{
//?do?something?here
i?=?i?+1;
}
}
-?(void)sampleForSwitch
{
SampleEnum?testEnum?=?SampleEnumTwo;
switch(testEnum)?{
caseSampleEnumUndefined:{
//?do?something
break;
}
caseSampleEnumOne:{
//?do?something
break;
}
caseSampleEnumTwo:{
//?do?something
break;
}
default:{
NSLog(@"WARNING:?there?is?an?enum?type?not?handled?properly!");
break;
}
}
任何需要寫(xiě)大括號(hào)的部分涝动,不得省略
錯(cuò)誤示例:
-?(void)wrongExamples
{
BOOLsomeCondition?=?YES;
if(someCondition)
NSLog(@"this?is?wrong!!!");
while(someCondition)
NSLog(@"this?is?wrong!!!");
}
注:以上摘自?http://www.code4app.com/blog-847095-1513.html