一.語言
采用US(美式)英語,不使UK(英式)英語或漢字拼音.
US: UIColor *myColor =[UIColor blueColor];
UK: UIColor *myColour =[UIColor blueColor];
拼音: UIColor *wodeYanSe =[UIColor blueColor];
二.命名規則
1.常量的命名
在前面加上小寫字母k作為標記.其余遵循小駝峰命名法(第一個單詞全部小寫,后面單詞首字母大寫).
NSTimeInterval kAnimationDuration = 0.3;
2.宏的命名
以兩個大寫字母作為前綴,后面遵循大駝峰命名法.
#define KKScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define KKAppVersion @"appVersion"
3.枚舉的命名
遵循Objective-C內部框架定義方式.
Enum中枚舉內容的命名需要以該Enum類型名稱開頭.
typedef NS_ENUM(NSInteger, FulowersMoveDestination)
{
FulowersMoveDestinationTop,
FulowersMoveDestinationBottom,
FulowersMoveDestinationLeft,
FulowersMoveDestinationRight,
};
4.類的命名
整體采用大駝峰式命名(每個單詞的首字母大寫).
類前綴:采用開發者姓名的首字母大寫.
類后綴:采用對應類的全稱.
NavigationController 導航控制器: LBYNavigationController
ViewController 主頁視圖控制器: LBYHomeViewController
TableViewController 表格控制器: LBYTableViewController
TabBarController 標簽控制器: LBYTabBarController
5.方法的命名
當方法參數在三個以及三個以上,換行保持對齊(冒號對齊,冒號前是參數變量,冒號后是參數值).
方法聲明:
+ (instancetype)initWithPersonName:(NSString *)name
withAge:(int)age
withSex:(NSString *)sex
withHeight:(float)height
withWeight:(float)weight;
方法調用:避免使用冒號對齊的方式.
6.屬性和對象的命名
采用修飾+類型的方式命名,BOOL類型添加is前綴,單詞遵循小駝峰命名法.聲明屬性時,小括號中的順序依次是:nonatomic,readonly,strong.
@property (nonatomic, assign) BOOL isLogin;
@property (nonatomic, weak) UITextField *loginNameTextField;
@property (nonatomic, copy) NSString *studentClientName;
@property (nonatomic, weak) UILabel *loginTipLabel;
@property (nonatomic, weak) UIButton *loginButton;
三.注釋
注釋是為了解釋說明,方法或變量等命名規范合理,清楚易懂,可以不添加注釋,含有復雜邏輯的代碼必須添加注釋.注釋需要實時更新,跟隨代碼的變動進行更改或者刪除.
1.公開類方法注釋
在.h文件中聲明類方法,采用文檔注釋,要寫明方法的具體作用,所有參數的含義以及返回的參數值.
/**
創建person對象的類方法
@param name 姓名
@param age 年齡
@param sex 性別
@param height 身高
@param weight 體重
@return 返回person類對象
*/
+ (instancetype)initWithPersonName:(NSString *)name
withAge:(int)age
withSex:(NSString *)sex
withHeight:(float)height
withWeight:(float)weight;
2.私有的對象方法注釋
在.m文件中實現對象方法,采用文檔注釋, 要寫明方法的具體作用, 如果有參數和返回值,需要添加所有參數的含義以及返回的參數值.
/**
搭建tableview的UI
*/
- (void)setupTableViewUI
3.方法內部邏輯代碼注釋
復雜邏輯代碼在代碼上方進行注釋,注釋方式采用雙斜杠+單個空格+具體注釋內容
- (void)viewDidLoad
{
[super ViewDidLoad];
// 注釋
if(...)
{
...
}
}
4.屬性注釋
/**
登陸按鈕
*/
@property (nonatomic, weak) UIButton *loginBtn;
5.標記
在函數分組和使用#pragma mark - 給重要邏輯代碼添加標記,方便閱讀
#pragma mark - Lifecycle
- (instancetype)init
- (void)dealloc
- (void)viewDidLoad
- (void)viewWillAppear:(BOOL)animated
- (void)didReceiveMemoryWarning
#pragma mark - IBActions
- (IBAction)submitData:(id)sender
#pragma mark - Public
- (void)publicMethod
#pragma mark - Private
- (void)privateMethod
#pragma mark - Custom Protocol
- (void)tabbarBottomView:(LBYTabbarBottomView *)tabbarBottomView didSelectIndex:(NSUInteger)index didSelectBtn:(BYBottomButton *)selectBtn
#pragma mark - UITextFieldDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
#pragma mark - NSObject
- (NSString *)description
6.打印
關于NSLog,在項目或者SDK完成最終版本時,需要去掉打印的注釋(可以使用pch預編譯文件來禁止NSLog打印)
四.格式化
1.賦值
在”=”號左右兩邊各間隔一個空格.
static const int count = 0;
類方法或對象方法
方法的”+/-”號和左側小括號間隔一個空格,大括號換行,上下大括號對齊.
- (void)viewDidLoad
{
}
2.屬性聲明
property和左側小括號間隔一個空格,逗號和下一個屬性修飾符間隔一個空格,右側小括號和屬性類型間隔一個空格,屬性類型和屬性變量間隔一個空格.聲明字符串類型時,NSString和號間隔一個空格,號和屬性變量相連,應是NSString *studentClientName
,不是NSString* studentClientName
,也不是NSString * studentClientName
.
@property (nonatomic, assign, readwrite) BOOL isLogin;
@property (nonatomic, copy, readwrite) NSString *studentClientName;
3.for循環
for和左側小括號間隔一個空格, i和”<=”間隔一個空格, ”<=”和”3”間隔一個空格, ”3”后面緊跟著封號 ,封號和i間隔一個空格.大括號換行,一對大括號上下位置對齊.
for (int i = 0; i <= 3; i++)
{
// 語句
}
4.條件語句
關于大括號,任何需要大括號的都不能省略.
采用
if (isLogin)
{
return success;
}
不是
if (isLogin)
return success;
也不是
if (isLogin) return success;
5.case語句
當一個case語句包含多行代碼時,大括號應該加上.如case 2所示.
switch (condition)
{
case 1:
// ...
break;
case 2:
{
// ...
// Multi-line example using braces
break;
}
case 3:
// ...
break;
default:
// ...
break;
}
當在switch使用枚舉類型時,default是不需要的.
switch (FulowersMoveDestination)
{
case FulowersMoveDestinationTop:
// ...
break;
case FulowersMoveDestinationBottom:
// ...
break;
case FulowersMoveDestinationLeft:
// ...
break;
case FulowersMoveDestinationRight:
// ...
break;
}
五.單例
統一采用shared+類名作為單例類的方法名
@implementation LBYNetworkTool
+ (instancetype)sharedBYNetworkTool
{
static LBYNetworkTool *instance;
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[LBYNetworkTool alloc]init];
});
}
return instance;
}
@end
六.UI整理
搭建UI時,使用setup作為方法名前綴,將相應UI布局放在對應方法中
- (void)setupTableViewUI
- (void)setupNavigationUI
- (void)setupCollectionViewUI