iOS開發代碼規範

來源:互聯網
上載者:User

標籤:

1、關於命名

1.1統一要求

  • 含義清楚, 盡量做到不需要注釋也能瞭解其作用,若做不到,就加註釋
  • 使用全稱不使用縮寫

1.2類的命名

  • 大駝峰式命名:每一個單詞的首字母都採用大寫字母
    例子: MFHomePageViewController
  • 尾碼要求
    • ViewController: 使用ViewController做尾碼
      例子: MFHomeViewController
    • View: 使用View做尾碼
      例子: MFAlertView
    • UITableCell:使用Cell做尾碼
      例子: MFNewsCell    
    • Protocol: 使用Delegate或者DataSource作為尾碼
      例子: UITableViewDelegate
    • UI控制項以此類推

1.3私人變數

  • 小駝峰式命名: 第一個單詞以小寫字母開始, 第二個單詞的首字母大寫
    例如:firstName、lastName
  • 以_開頭,第一個單詞首字母小寫
    例子: NSString * somePrivateVariable
  • 私人變數放在.m檔案中聲明

1.4 property變數

  • 小駝峰式命名
    例子: @property (nonatomic, copy) NSString *userName;
  • 禁止使用synthesize關鍵詞

1.5宏命名

  • 全部大寫, 單詞間用_分隔。[不帶參數]
    例子: #define THIS_IS_AN_MACRO @” THIS_IS_AN_MACRO”
  • 駝峰命名。[帶參數]
    #define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]

1.6 Enum

  • Enum類型的命名與類的命名規則一致
  • Enum中枚舉內容的命名需要以該Enum類型名稱開頭
    例子
    1    typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {2     AFNetworkReachabilityStatusUnknown          = -1,3     AFNetworkReachabilityStatusNotReachable     = 0,4     AFNetworkReachabilityStatusReachableViaWWAN = 1,5     AFNetworkReachabilityStatusReachableViaWiFi = 2,6 };

1.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、私人方法及變數聲明

2.1聲明位置
    在.m檔案中最上方,定義空的category進行聲明
例子:

 1 #import "CodeStandardViewController.h" 2 //define your private variables and methods here 3 @interface CodeStandardViewController () 4 { 5 } 6 - (void)samplePrivateMethod; 7 @end 8  9 #define THIS_IS_AN_SAMPLE_MACRO @"THIS_IS_AN_SAMPLE_MACRO"10 @implementation CodeStandardViewController11 #pragma mark - private methods12 - (void)samplePrivateMethod13 {14     //some code15 }
3、關於注釋

最好的代碼是不需要注釋的,盡量通過合理的命名,良好的代碼把含義表達清楚,在必要的地方添加註釋。
注釋需要與代碼同步更新。
如果做不到命名盡量的見名知意的話,就可以適當的添加一些注釋或者mark。
3.1 屬性注釋例子:
 /// 學生
 @property (nonatomic, strong) Student *student;
3.2 方法聲明注釋:
/**
 *  @brief  登入驗證
 *
 *  @param  personId 使用者名稱
 *  @param  password 密碼
 *  @param  complete 執行完畢的block
 *
 *  @return
 */
+ (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;

4、格式化代碼

4.1指標 "*" 位置
      例子: NSString *userName;
4.2方法的聲明和定義
      在 - 、+ 和傳回值之間留一個空格,方法名和第一個參數之間不留空格
      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    ...
  }
4.3代碼縮排

  • 使用xcode預設縮排,即 tab = 4空格
  • 使用xcode中re-indent功能定期對代碼格式進行整理
  • 相同類型變數聲明需要獨行聲明
    例子:
    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.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{    ...}

4.5大括弧寫法

  • 對於類的method: 左括弧另起一行寫

  例子:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}

 

  • 對於其他使用情境: 左括弧跟在第一行後邊
    例子
      - (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) {          case SampleEnumUndefined:{              // do something              break;          }          case SampleEnumOne:{              // do something              break;          }          case SampleEnumTwo:{              // do something              break;          }          default:{              NSLog(@"WARNING: there is an enum type not handled   properly!");              break;          }      }
  •  任何需要寫大括弧的部分,不得省略


 

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.