iOS開發代碼規範

來源:互聯網
上載者:User

標籤:

1. 關於命名 1> 統一要求
  • 含義清楚,盡量做到不需要注釋也能瞭解其作用,若做不到,就加註釋

  • 使用全稱,不適用縮寫

 2> 類的命名
  • 大駝峰式命名:每個單詞的首字母都採用大寫字母

   例子:MFHomePageViewController

  • 尾碼要求

  ViewController: 使用ViewController做尾碼

       例子: MFHomeViewController

     View: 使用View做尾碼

       例子: MFAlertView

     UITableCell:使用Cell做尾碼

       例子: MFNewsCell

     Protocol: 使用Delegate或者DataSource作為尾碼

       例子: UITableViewDelegate

     UI控制項依次類推

 3> 私人變數
  • 小駝峰式命名:第一個單詞以小寫字母開始,後面的單詞的首字母全部大寫

  例子:firstName、lastName

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

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

  例子:

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開發代碼規範

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.