? 此文整理是為了方便一些朋友規(guī)范代碼適用于新人(ps:如果你已經(jīng)很規(guī)范的執(zhí)行了那么可以跳過此內(nèi)容)這樣以來如果有新人入職需要熟悉代碼的話,對(duì)他來說也是一件好事情,之前因?yàn)榇蟛糠猪?xiàng)目都是個(gè)人獨(dú)立開發(fā),內(nèi)容大都遵循此規(guī)范(當(dāng)然也有趕進(jìn)度的時(shí)候就寫的比較隨意一些了),近期公司擴(kuò)展部門人員,正好工作也空閑下來就整理了一下內(nèi)容。
廢話不說了,開始正文,如有未完善的內(nèi)容還望各位同仁補(bǔ)充一下,謝謝??。
1. 關(guān)于命名
1> 統(tǒng)一要求
含義清楚,盡量做到不需要注釋也能了解其作用,若做不到,就加注釋
使用全稱,不適用縮寫
2> 類的命名
大駝峰式命名:每個(gè)單詞的首字母都采用大寫字母
例子:MFHomePageViewController
后綴要求
ViewController: 使用ViewController做后綴
例子:MFHomeViewController
View: 使用View做后綴
例子:MFAlertView
UITableCell:使用Cell做后綴
例子:MFNewsCell
Protocol: 使用Delegate或者DataSource作為后綴
例子:UITableViewDelegate
UI控件依次類推
3> 私有變量
小駝峰式命名:第一個(gè)單詞以小寫字母開始,后面的單詞的首字母全部大寫
例子:firstName、lastName
以_開頭,第一個(gè)單詞首字母小寫
例子:NSString *_somePrivateVariable
私有變量放在.m文件中聲明
4>?property變量
小駝峰式命名
例子:///注釋
@property (nonatomic, copy) NSString *userName;
禁止使用synthesize關(guān)鍵詞
5> 宏命名
全部大寫,單詞間用_分隔。[不帶參數(shù)]
例子: #defineTHIS_IS_AN_MACRO@"THIS_IS_AN_MACRO"
以字母k開頭,后面遵循大駝峰命名。[不帶參數(shù)]
例子:#definekWidthself.frame.size.width
小駝峰命名。[帶參數(shù)]
#definegetImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
6>?Enum
Enum類型的命名與類的命名規(guī)則一致
Enum中枚舉內(nèi)容的命名需要以該Enum類型名稱開頭
例子:
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable =0,
AFNetworkReachabilityStatusReachableViaWWAN =1,
AFNetworkReachabilityStatusReachableViaWiFi =26
};
7> Delegate命名
類的實(shí)例必須為回調(diào)方法的參數(shù)之一, 如
-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section
回調(diào)方法的參數(shù)只有類自己的情況,方法名要符合實(shí)際含義, 如:
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
以類的名字開頭(回調(diào)方法存在兩個(gè)以上參數(shù)的情況)以表明此方法是屬于哪個(gè)類的, 如:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
使用did和will通知Delegate已經(jīng)發(fā)生的變化或?qū)⒁l(fā)生的變化, 如:
-(NSIndexPath*)tableView:(UITableView*)tableViewwillSelectRowAtIndexPath:(NSIndexPath*)indexPath;
-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath;
2. 私有方法及變量聲明
1> 聲明位置
在.m文件中最上方,定義空的category進(jìn)行聲明
例子:
#import"CodeStandardViewController.h"
//在這個(gè)category(類目)中定義變量和方法
@interfaceCodeStandardViewController ()
{//聲明私有變量
}
//私有方法
- (void)samplePrivateMethod;
@end
@implementationCodeStandardViewController
//私有方法的實(shí)現(xiàn)
- (void)samplePrivateMethod
{
//some code
}
3. 關(guān)于注釋
最好的代碼是不需要注釋的 盡量通過合理的命名
良好的代碼把含義表達(dá)清楚 在必要的地方添加注釋
注釋需要與代碼同步更新
如果做不到命名盡量的見名知意的話,就可以適當(dāng)?shù)奶砑右恍┳⑨尰蛘適ark
1> 屬性注釋
例子:
/// 學(xué)生
@property (nonatomic, strong) Student *student;
2> 方法聲明注釋:
/**
* @brief 登錄驗(yàn)證
*
* @param personId 用戶名
* @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. 格式化代碼
1> 指針 "*" 位置
定義一個(gè)對(duì)象時(shí),指針 "*" 靠近變量
例子: NSString*userName;
2> 方法的聲明和定義
在 - 、+ 和 返回值 之間留一個(gè)空格,方法名和第一個(gè)參數(shù)之間不留空格
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{...}
3> 代碼縮進(jìn)
使用 xcode 默認(rèn)縮進(jìn),即tab = 4空格
使用 xcode 中re-indent功能定期對(duì)代碼格式進(jìn)行整理
相同類型變量聲明需要獨(dú)行聲明
例子:
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> 對(duì)method進(jìn)行分組
使用#pragma mark -方式對(duì)類的方法進(jìn)行分組
例子:
#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> 大括號(hào)寫法
對(duì)于類的method: 左括號(hào)另起一行寫(遵循蘋果官方文檔)
例子:
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{
? ? ? ? ? self = [super initWithNibName:nibNameOrNilbundle:nibBundleOrNil];
? ? ? ? ? if (self) {
? ? ? ? ? ? ? ? ? // Custom initialization
? ? ? ? ? ? ?}
? ? ? ? ? ? return self;
}
對(duì)于其他使用場(chǎng)景: 左括號(hào)跟在第一行后邊
例子:
- (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) {
? ? ? ? ? ? ? ? ? ? ? caseSampleEnumUndefined:{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// do something
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? caseSampleEnumOne:{
? ? ? ? ? ? ? ? ? ? ? ? ? ? // do something
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? caseSampleEnumTwo:{
? ? ? ? ? ? ? ? ? ? ? ? ? ? // do something
? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ?default:{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NSLog(@"WARNING: there is an enum type not handled properly!");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ?}
}
任何需要寫大括號(hào)的部分,不得省略
錯(cuò)誤示例:
- (void)wrongExamples
{
BOOLsomeCondition = YES;
if (someCondition)
NSLog(@"this is wrong!!!");
while(someCondition)
NSLog(@"this is wrong!!!");
}