一、命名原則
1.一般性原則:可讀性高(簡潔且清晰)和防止命名沖突(通過加前綴后綴來保證)。
大駝峰式命名:每個單詞的首字母都采用大寫字母
例子:
WYHomePageViewController
2.一致性
盡可能與Cocoa
編程接?命名保持一致。如果你不太確定某個命名的?致性,請瀏覽頭文件或參考文檔中的范例,在使?多態(tài)方法的類中,命名的?致性?常重要。在不同類中實現(xiàn)相同功能的?法應(yīng)該具有同的名稱。
3.類的命名
a、后綴要求
ViewController: 使用ViewController做后綴
例子: MFHomeViewController
View: 使用View做后綴
例子: MFAlertView
UITableCell:使用Cell做后綴
例子: MFNewsCell
Protocol: 使用Delegate或者DataSource作為后綴
例子: UITableViewDelegate
UI控件依次類推
b、 私有變量
小駝峰式命名:第一個單詞以小寫字母開始,后面的單詞的首字母全部大寫
例子:firstName、lastName
以 _ 開頭,第一個單詞首字母小寫
例子:NSString * _somePrivateVariable
私有變量放在 .m 文件中聲明
c、 property變量
小駝峰式命名
例子:///注釋
@property (nonatomic, copy) NSString *userName;
禁止使用synthesize關(guān)鍵詞
d、 宏命名
全部大寫,單詞間用 _ 分隔。[不帶參數(shù)]
例子: #define THIS_IS_AN_MACRO @"THIS_IS_AN_MACRO"
以字母 k 開頭,后面遵循大駝峰命名。[不帶參數(shù)]
例子:#define kWidth self.frame.size.width
小駝峰命名。[帶參數(shù)]
#define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
e、 Enum
Enum類型的命名與類的命名規(guī)則一致
Enum中枚舉內(nèi)容的命名需要以該Enum類型名稱開頭
例子:
1 typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
2 AFNetworkReachabilityStatusUnknown = -1,
3 AFNetworkReachabilityStatusNotReachable = 0,
4 AFNetworkReachabilityStatusReachableViaWWAN = 1,
5 AFNetworkReachabilityStatusReachableViaWiFi = 2
6 };
f、 Delegate命名
類的實例必須為回調(diào)方法的參數(shù)之一, 如
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
回調(diào)方法的參數(shù)只有類自己的情況,方法名要符合實際含義, 如:
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
以類的名字開頭(回調(diào)方法存在兩個以上參數(shù)的情況)以表明此方法是屬于哪個類的, 如:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
使用did和will通知Delegate已經(jīng)發(fā)生的變化或?qū)⒁l(fā)生的變化, 如:
-(NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath;
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
私有方法的命名
在.m文件中最上方,定義空的category進行聲明
例子:
#import "CodeStandardViewController.h"
// 在這個category(類目)中定義變量和方法 3
@interface CodeStandardViewController ()
{
// 聲明私有變量
}
// 私有方法
- (void)samplePrivateMethod;
@end
@implementation CodeStandardViewController
// 私有方法的實現(xiàn)
- (void)samplePrivateMethod
{
//some code
}
關(guān)于注釋
最好的代碼是不需要注釋的 盡量通過合理的命名
良好的代碼把含義表達清楚 在必要的地方添加注釋
注釋需要與代碼同步更新
如果做不到命名盡量的見名知意的話,就可以適當(dāng)?shù)奶砑右恍┳⑨尰蛘適ark
1> 屬性注釋
例子:
/// 學(xué)生
@property (nonatomic, strong) Student *student;
2>方法聲明注釋
/**
* @brief 登錄驗證
*
* @param personId 用戶名
* @param password 密碼
* @param complete 執(zhí)行完畢的block
*
* @return
*/
+ (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;
格式化代碼
#####1> 指針 "" 位置
定義一個對象時,指針 "" 靠近變量
例子: NSString *userName;
2> 方法的聲明和定義
在 - 、+ 和 返回值 之間留一個空格,方法名和第一個參數(shù)之間不留空格
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{...}
3> 代碼縮進
使用 xcode 默認縮進,即 tab = 4空格
使用 xcode 中 re-indent 功能定期對代碼格式進行整理
相同類型變量聲明需要獨行聲明
例子:
CGFloatoringX = frame.origin.x;
CGFloatoringY = frame.origin.y;
CGFloatlineWidth = frame.size.width;
Method與Method之間空一行
例子:
#pragma mark - private methods
- (void)samplePrivateMethod
{...}
- (void)sampleForIf
{...}
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
{...}
復(fù)制代碼
5> 大括號寫法
對于類的method: 左括號另起一行寫(遵循蘋果官方文檔)
例子:
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
對于其他使用場景: 左括號跟在第一行后邊
例子:
1 - (void)sampleForIf
2 {
3 BOOL someCondition = YES;
4 if(someCondition) {
5 // do something here
6 }
7 }
8 - (void)sampleForWhile
9 {
10 int i = 0;
11 while (i < 10) {
12 // do something here
13 i = i + 1;
14 }
15 }
16 - (void)sampleForSwitch
17 {
18 SampleEnum testEnum = SampleEnumTwo;
19 switch(testEnum) {
20 caseSampleEnumUndefined:{
21 // do something
22 break;
23 }
24 caseSampleEnumOne:{
25 // do something
26 break;
27 }
28 caseSampleEnumTwo:{
29 // do something
30 break;
31 }
32 default:{
33 NSLog(@"WARNING: there is an enum type not handled properly!");
34 break;
35 }
36 }