標籤:
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代碼縮排
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大括弧寫法
例子:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}
iOS開發代碼規範