iOS開發代碼規范(通用)


1. 關於命名

 1> 統一要求

  • 含義清楚盡量做到不需要注釋也能了解其作用,若做不到,就加注釋

  • 使用全稱,不適用縮寫

 2> 類的命名

  • 大駝峰式命名:每個單詞的首字母都采用大寫字母

   例子:MFHomePageViewController

  • 后綴要求

  ViewController: 使用ViewController做后綴

       例子: MFHomeViewController

     View: 使用View做后綴

       例子: MFAlertView

     UITableCell:使用Cell做后綴

       例子: MFNewsCell

     Protocol: 使用Delegate或者DataSource作為后綴

       例子: UITableViewDelegate

     UI控件依次類推

 3> 私有變量

  • 小駝峰式命名:第一個單詞以小寫字母開始,后面的單詞的首字母全部大寫

  例子:firstNamelastName

  • _ 開頭,第一個單詞首字母小寫

  例子:NSString * _somePrivateVariable

  • 私有變量放在 .m 文件中聲明 

 4> property變量

  • 小駝峰式命名

   例子:///注釋

      @property (nonatomic, copy) NSString *userName;

  • 禁止使用synthesize關鍵詞

 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

  • Enum類型的命名與類的命名規則一致

  • Enum中枚舉內容的命名需要以該Enum類型名稱開頭

  例子:

1 typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
2     AFNetworkReachabilityStatusUnknown = -1,
3     AFNetworkReachabilityStatusNotReachable = 0,
4     AFNetworkReachabilityStatusReachableViaWWAN = 1,
5     AFNetworkReachabilityStatusReachableViaWiFi = 2
6     };

 7> Delegate命名

  • 類的實例必須為回調方法的參數之一, 如

  -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

  • 回調方法的參數只有類自己的情況,方法名要符合實際含義, 如:

  -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView

  • 以類的名字開頭(回調方法存在兩個以上參數的情況)以表明此方法是屬於哪個類的, 如:

  -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  • 使用didwill通知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     @end
12 
13     @implementation CodeStandardViewController
14     // 私有方法的實現
15     - (void)samplePrivateMethod
16     {
17       //some code
18     }

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;
  • Method與Method之間空一行

  例子:

1 #pragma mark - private methods
2 
3 - (void)samplePrivateMethod
4 {...}
5 
6 - (void)sampleForIf
7 {...}

 4> 對method進行分組

  使用 #pragma mark - 方式對類的方法進行分組

   例子:

 1      #pragma mark - private methods
 2 
 3   - (void)samplePrivateMethod
 4   {...}
 5 
 6   - (void)sampleForIf
 7   {...}
 8 
 9   - (void)sampleForWhile
10   {...}
11   
12   - (void)sampleForSwitch
13   {...}
14 
15   - (void)wrongExamples
16   {...}
17 
18   #pragma mark - public methods
19   - (void)samplePublicMethodWithParam:(NSString*)sampleParam
20   {...}
21 
22   #pragma mark - life cycle methods
23   - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
24   {...}
25 
26   - (void)viewDidLoad
27   {...}
28 
29   - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
30   {...}

 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 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     }
  • 任何需要寫大括號的部分,不得省略

  錯誤示例:

1 - (void)wrongExamples
2 {
3     BOOLsomeCondition = YES;
4     if (someCondition)
5         NSLog(@"this is wrong!!!");
6     while(someCondition)
7         NSLog(@"this is wrong!!!");
8 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM