iOS代碼規范 beta 0.1

1. 關于命名

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

2>類的命名

1.大駝峰式命名:每個單詞的首字母都采用大寫字母;
2.使用前綴IFS+模塊+?名稱+后綴的方式命名;
例:IFSHomePageViewController

3>后綴要求

1.ViewController: 使用ViewController做后綴
例:MFHomeViewController

2.View: 使用View做后綴
例:MFAlertView

3.UITableCell:使用Cell做后綴
例:MFNewsCell

4.其它類推

5>property變量命名

1.小駝峰式命名
2.適當使用控件類型縮寫以表明變量所屬控件類型,如:

UIButton -> Btn
UILabel -> L
UITextField -> TF
@property (nonatomic, copy) NSString *userName;
@property (nonatomic, weak) UILabel  *userNameL;
@property (nonatomic, weak) UIButton *loginBtn;

6>?常量命名

1.盡量不用#define預處理指令;
2.以字母 k 開頭,后面遵循大駝峰命名;

//私有常量 
static const NSTimeInterval kAnimationDuration = 0.3; //.m定義即可

//全局數值型常量
extern const NSTimeInterval kAnimationDuration; //.h聲明
const NSTimeInterval kAnimationDuration = 0.3; //.m實現

//全局?字符串型常量
extern NSString * const kEOCStringConstant; //.h聲明
NSString * const kEOCStringConstant = @"value";//.m實現

7> Enum

1.Enum類型的命名與類的命名規則一致
2.Enum中枚舉內容的命名需要以該Enum類型名稱開頭

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

8> Delegate命名

1.以類名開頭;
2.將類實例以參數的形式返回出來;
3.參考UITableViewDelegate方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

3. 關于注釋

1最好的代碼是不需要注釋的,盡量通過合理的命名、良好的代碼把含義表達清楚;
2.注釋需要與代碼同步更新
3.如果做不到命名盡量的見名知意的話,就可以適當的添加一些注釋或者mark

1>屬性注釋

/** 學生 */
@property (nonatomic, strong) Student *student;

2>方法聲明注釋:

/**
* @brief 登錄驗證
*
* @param personId 用戶名
* @param password 密碼
* @param complete 執行完畢的block
*
* @return
*/
+ (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;

4. 關于UI布局

1.xib + autolayout
2.Masonry
3.不建議使用手寫frame的方式進行控件布局;

5. 格式化代碼

1>指針 "*" 位置

1.定義變量:
NSString *userName;
NSString[空格]*userName;

2.定義property:
@property (nonatomic, copy) Foo *foo;
@property[空格](nonatomic,[空格]copy)[空格]Foo[空格]*foo;

3.定義?方法:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
-[空格](id)initWithNibName:(NSString[空格]*)nibNameOrNil[空格]bundle:(NSBundle[空格]*)nibBundleOrNil;

3>代碼縮進

1.使用tab鍵進行代碼縮進
2.定期使用Editor -> Structure -> Re-Indent 功能?進行排版格式化處理;

4>對method進行分組

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

#pragma mark - life cycle methods
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {...}
- (void)viewDidLoad {...}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {...}

#pragma mark - public methods 
- (void)samplePublicMethodWithParam:(NSString*)sampleParam {...}

#pragma mark - private methods
- (void)samplePrivateMethod {...}
- (void)sampleForIf {...}
- (void)sampleForWhile {...}
- (void)sampleForSwitch {...}
- (void)wrongExamples {...}

5>大括號寫法

1.對于method: 左括號另起一行寫

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

2.其他?情?形: 左括號跟在第一行后邊,空格+{

- (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) {
    caseSampleEnumUndefined: {
      // do something
      break;
    }
    caseSampleEnumOne: {
      // do something
      break;
    }
    caseSampleEnumTwo: {
      // do something
      break;
    }
    default: {
      NSLog(@"WARNING: there is an enum type not handled properly!");
      break;
    }
}

6>ViewController代碼組織結構

#import "SomeModel.h"
#import "SomeView.h"
#import "SomeController.h"
#import "SomeStore.h"
#import "SomeHelper.h"
#import <SomeExternalLibrary/SomeExternalLibraryHeader.h>

static NSString * const ?IFSFooStringConstant = @"FoobarConstant";
static CGFloat const ?IFSFooFloatConstant = 1234.5;

@interface ?IFSFooViewController () <?IFSBarDelegate>
@property (nonatomic, copy, readonly) Foo *foo;
@end

@implementation IFSFooViewController

#pragma mark - Lifecycle
- (void)dealloc {...}
- (instancetype)initWithFoo:(Foo *)foo {...}
 
#pragma mark - View Lifecycle
- (void)viewWillAppear:(BOOL)animated {...}
- (void)viewDidLoad {...}

#pragma mark - Layout
- (void)makeViewConstraints {...}

#pragma mark - Public Interface
- (void)startFooing {...}
- (void)stopFooing {...}

#pragma mark - User Interaction
- (void)btnOnclick {...}

#pragma mark - Private Methods
- (NSString *)displayNameForFoo:(Foo *)foo {...}

#pragma mark - IFSBarDelegate
- (void)foobar:(Foobar *)foobar didSomethingWithFoo:(Foo *)foo {...}

@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • iOS編程規范0規范 0.1前言 為??高產品代碼質量,指導廣大軟件開發人員編寫出簡潔、可維護、可靠、可 測試、高效...
    iOS行者閱讀 4,502評論 21 35
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 代碼格式 使用空格而不是制表符 Tab 不要在工程里使用 Tab 鍵,使用空格來進行縮進。在 Xcode > Pr...
    small_Sun閱讀 1,383評論 1 3
  • Main Features 基于微信公眾號Web端及iOS端的社交產品 身份認證:工卡照、企業郵箱、ip地址 個人...
    huglee閱讀 825評論 0 0
  • 感恩惠家慈善基金主辦,及種子太太的帶領,再次有機會成長者探 今天金剛義工隊有四十多人,老中青三代,一同為愛而出發。...
    潔雯閱讀 218評論 0 3