標籤:
1. 關於命名 1> 統一要求
2> 類的命名
例子:MFHomePageViewController
ViewController: 使用ViewController做尾碼
例子: MFHomeViewController
View: 使用View做尾碼
例子: MFAlertView
UITableCell:使用Cell做尾碼
例子: MFNewsCell
Protocol: 使用Delegate或者DataSource作為尾碼
例子: UITableViewDelegate
UI控制項依次類推
3> 私人變數
- 小駝峰式命名:第一個單詞以小寫字母開始,後面的單詞的首字母全部大寫
例子:firstName、lastName
例子:NSString * _somePrivateVariable
4> property變數
例子:///注釋
@property (nonatomic, copy) NSString *userName;
5> 宏命名
例子: #define THIS_IS_AN_MACRO @"THIS_IS_AN_MACRO"
- 以字母 k 開頭,後面遵循大駝峰命名。[不帶參數]
例子:#define kWidth self.frame.size.width
#define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
6> Enum
例子:
1 typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {2 AFNetworkReachabilityStatusUnknown = -1,3 AFNetworkReachabilityStatusNotReachable = 0,4 AFNetworkReachabilityStatusReachableViaWWAN = 1,5 AFNetworkReachabilityStatusReachableViaWiFi = 26 }; 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. 私人方法及變數聲明
1> 聲明位置
在.m檔案中最上方,定義空的category進行聲明
例子:
1 #import "CodeStandardViewController.h" 2 // 在這個category(類目)中定義變數和方法 3 @interface CodeStandardViewController () 4 { 5 6 // 聲明私人變數 7 } 8 9 // 私人方法10 - (void)samplePrivateMethod;11 @end12 13 @implementation CodeStandardViewController14 // 私人方法的實現15 - (void)samplePrivateMethod16 {17 //some code18 }3. 關於注釋
最好的代碼是不需要注釋的 盡量通過合理的命名
良好的代碼把含義表達清楚 在必要的地方添加註釋
注釋需要與代碼同步更新
如果做不到命名盡量的見名知意的話,就可以適當的添加一些注釋或者mark
1> 屬性注釋
例子:
/// 學生
@property (nonatomic, strong) Student *student;
2> 方法聲明注釋:
1 /** 2 * @brief 登入驗證 3 * 4 * @param personId 使用者名稱 5 * @param password 密碼 6 * @param complete 執行完畢的block 7 * 8 * @return 9 */10 + (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;
4. 關於UI布局
使用Interface Builder進行介面布局
Xib檔案的命名與其對應的.h檔案保持相同
Xib檔案中控制項的組織圖要合理,Xib檔案中控制項需要有合理的可讀性強的命名,方便他人理解
5. 格式化代碼 1> 指標 "*" 位置
定義一個對象時,指標 "*" 靠近變數
例子: NSString *userName;
2> 方法的聲明和定義
在 - 、+ 和 傳回值 之間留一個空格,方法名和第一個參數之間不留空格
- (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;
例子:
1 #pragma mark - private methods2 3 - (void)samplePrivateMethod4 {...}5 6 - (void)sampleForIf7 {...} 4> 對method進行分組
使用 #pragma mark - 方式對類的方法進行分組
例子:
1 #pragma mark - private methods 2 3 - (void)samplePrivateMethod 4 {...} 5 6 - (void)sampleForIf 7 {...} 8 9 - (void)sampleForWhile10 {...}11 12 - (void)sampleForSwitch13 {...}14 15 - (void)wrongExamples16 {...}17 18 #pragma mark - public methods19 - (void)samplePublicMethodWithParam:(NSString*)sampleParam20 {...}21 22 #pragma mark - life cycle methods23 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil24 {...}25 26 - (void)viewDidLoad27 {...}28 29 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation30 {...} 5> 大括弧寫法
- 對於類的method: 左括弧另起一行寫(遵循蘋果官方文檔)
例子:
1 - (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil 2 { 3 self = [super initWithNibName:nibNameOrNil 4 5 bundle:nibBundleOrNil]; 6 7 if (self) { 8 // Custom initialization 9 }10 11 return self;12 }
例子:
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 here13 i = i + 1;14 }15 }16 - (void)sampleForSwitch17 {18 SampleEnum testEnum = SampleEnumTwo;19 switch(testEnum) {20 caseSampleEnumUndefined:{21 // do something22 break;23 }24 caseSampleEnumOne:{25 // do something26 break;27 }28 caseSampleEnumTwo:{29 // do something30 break;31 }32 default:{33 NSLog(@"WARNING: there is an enum type not handled properly!");34 break;35 }36 }
錯誤樣本:
1 - (void)wrongExamples2 {3 BOOLsomeCondition = YES;4 if (someCondition)5 NSLog(@"this is wrong!!!");6 while(someCondition)7 NSLog(@"this is wrong!!!");8 }
iOS開發代碼規範