一.注釋規范
1.創建類添加類注釋
/**
*<#空格#>SanW--2017.5.3--公共參數
*/
@interface PTCommonParams : NSObject
2.屬性注釋
/**
*<#空格#>設備系統版本
*/
(///<#空格#>設備系統版本)
@property<#空格#> (nonatomic,copy)<#空格#>NSString<#空格#>*osVersion;
3.更改對方代碼時打上注釋
#warning mark<#空格#>- SanW--2017.5.3--解決數組越界奔潰(原因)
4.創建變量
//<#空格#>是否退出(變量以下劃線_開頭)
BOOL<#空格#>_isQuit;
二.命名規范(駝峰命名法)
1.It is good to be both clear and brief as possible, but clarity shouldn’t suffer because of brevity:
2.In general, don’t abbreviate names of things. Spell them out, even if they’re long:
3.However, a handful of abbreviations are truly common and have a long history of use. You can continue to use them; see Acceptable Abbreviations and Acronyms.
4.Avoid ambiguity in API names, such as method names that could be interpreted in more than one way.
1.類,方法,屬性,變量命名
//<#空格#>類命名一般添加個人/公司/應用程序大寫字母作為前綴
PTHoneController,PTUserModel,PTHeaderView
//<#空格#>方法命名主要從 --要什么 -- 做什么
-<#空格#>(NSString<#空格#>*)itemNamed:(NSString<#空格#>*)name
-<#空格#>(NSString<#空格#>*)findItemWithName:(NSString<#空格#>*)name
-<#空格#>(NSString<#空格#>*)itemAtIndex:(NSUInteger)index
//<#空格#>變量名稱以下劃線開頭_
BOOL<#空格#>_isQuit;
2.枚舉表示狀態、選項
定義的枚舉類型名稱應以 2~3 個大寫字母開頭,而這通常與項目設置的類文件前綴相同,跟隨其后的命名應采用駝峰命名法則,命名應準確表述枚舉表示的意義,枚舉中各個值都應以定義的枚舉類型開頭,其后跟隨各個枚舉值對應的狀態、選項或者狀態碼。
// 名稱以類名前綴+表示的意義 值:枚舉名+對應的狀態、選項或者狀態碼
typedef NS_ENUM(NSInteger,ReportHeaderType) {
ReportHeaderTypeAudio, //<#空格#>音頻
ReportHeaderTypeVideo //<#空格#>視頻
};
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3
};
3.常量
最好以宏代替部分方法,常量用const去定義
- 1> 使用宏預處理定義以k+代表意義定義,這樣定義的常量無類型信息,且如果你在調試時想要查看 kRadius 的值卻無從下手,因為在預處理階段kRadius 就已經被替換了,不便于調試
//<#空格#>圓角弧度
#define kRadius 6
- 2> 使用類型常量為常量帶入類型信息
//<#空格#>通常以個人/公司/類名大寫字母作為前綴+代表的意義+Const后綴
const<#空格#>NSTimeInterval<#空格#>PTAnimationDurationConst<#空格#>=<#空格#>0.3;
NSString<#空格#>*const<#空格#>UIApplicationLaunchOptionsLocalNotificationKey;
//<#空格#>消息區分私信
.h
extern<#空格#>NSString<#空格#>*const<#空格#>MESSAGE_LIVE_TYPE;
.m
NSString<#空格#>*const<#空格#>MESSAGE_LIVE_TYPE<#空格#>=<#空格#>@"直播間主播私信";
4.分類及其分類方法
分類名稱以個人/公司/類名大寫字母作為前綴+系統類名+ Ex后綴,分類中的方法名以個人/公司/類名小寫字母作為前綴,且以下劃線連接前綴與方法名。
UIColor+PTColorEx.h
//<#空格#>16進制顏色轉換
+<#空格#>(UIColor<#空格#>*)pt_colorWithHexString:(NSString<#空格#>*)hexString;
5.block和協議命名
- Block
//<#空格#>block表示的意義首字母大寫 + Block后綴
@property<#空格#>(nonatomic,copy)<#空格#>void<#空格#>(^ChooseClassfyBlock)(PTLiveClassfyModel<#空格#>*classfyInfo,UIButton<#空格#>*sender);
- 2.代理協議
//<#空格#>使用protocol定義代理協議時類名+Delegate 后綴
@protocol<#空格#>PTHomeViewDelegate<#空格#><NSObject>
6.判斷(switch/ if.....else)
在項目中盡量減少if...else 的使用,使用switch便利,相對而言if條件判斷,switch匹配