一枫夺、關(guān)于命名
1> 統(tǒng)一要求
含義清楚文判,盡量做到不需要注釋也能了解其作用胧谈,若做不到忆肾,就加注釋使用全稱,不適用縮寫
2> 類的命名
大駝峰式命名:每個單詞的首字母都采用大寫字母
例子:MFHomePageViewController
后綴要求
ViewController: 使用ViewController做后綴
例子: MFHomeViewController
View: 使用View做后綴
例子: MFAlertView
UITableCell:使用Cell做后綴
例子: MFNewsCell
Protocol: 使用Delegate或者DataSource作為后綴
例子: UITableViewDelegate
UI控件依次類推
3> 私有變量
小駝峰式命名:第一個單詞以小寫字母開始菱肖,后面的單詞的首字母全部大寫
例子:firstName客冈、lastName
以 _ 開頭,第一個單詞首字母小寫
例子:NSString * _somePrivateVariable
私有變量放在 .m 文件中聲明
4> property變量
小駝峰式命名
例子:///注釋
@property (nonatomic, copy) NSString *userName;
禁止使用synthesize關(guān)鍵詞
5> 宏命名
全部大寫稳强,單詞間用 _ 分隔场仲。[不帶參數(shù)]
例子: #define THIS_IS_AN_MACRO @"THIS_IS_AN_MACRO"
以字母 k 開頭,后面遵循大駝峰命名退疫。[不帶參數(shù)]
例子:#define kWidth self.frame.size.width
小駝峰命名渠缕。[帶參數(shù)]
#define getImageUrl(url) [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrl,url]]
6> Enum
Enum類型的命名與類的命名規(guī)則一致
Enum中枚舉內(nèi)容的命名需要以該Enum類型名稱開頭
例子:
typedef NS_ENUM(NSInteger, AFNetworkReachabilityStatus) {
AFNetworkReachabilityStatusUnknown = -1,
AFNetworkReachabilityStatusNotReachable = 0,
AFNetworkReachabilityStatusReachableViaWWAN = 1,
AFNetworkReachabilityStatusReachableViaWiFi = 26
};
7> Delegate命名
類的實例必須為回調(diào)方法的參數(shù)之一, 如
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
回調(diào)方法的參數(shù)只有類自己的情況,方法名要符合實際含義, 如:
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
以類的名字開頭(回調(diào)方法存在兩個以上參數(shù)的情況)以表明此方法是屬于哪個類的, 如:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
使用did和will通知Delegate已經(jīng)發(fā)生的變化或?qū)⒁l(fā)生的變化, 如:
-(NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath;
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;
二. 私有方法及變量聲明
1> 聲明位置
在.m文件中最上方褒繁,定義空的category進行聲明
例子:
#import "CodeStandardViewController.h"
// 在這個category(類目)中定義變量和方法
@interface CodeStandardViewController ()
{
// 聲明私有變量
}
// 私有方法
- (void)samplePrivateMethod;
@end
@implementation CodeStandardViewController
// 私有方法的實現(xiàn)
- (void)samplePrivateMethod
{
//some code
}
三. 關(guān)于注釋
最好的代碼是不需要注釋的 盡量通過合理的命名
良好的代碼把含義表達清楚 在必要的地方添加注釋
注釋需要與代碼同步更新
如果做不到命名盡量的見名知意的話亦鳞,就可以適當?shù)奶砑右恍┳⑨尰蛘適ark
1> 屬性注釋
例子:
/** 學(xué)生 */
@property (nonatomic, strong) Student *student;
2> 方法聲明注釋:
/**
* @brief 登錄驗證
*
* @param personId 用戶名
* @param password 密碼
* @param complete 執(zhí)行完畢的block
*
* @return
*/
+ (void)loginWithPersonId:(NSString *)personId password:(NSString *)password complete:(void (^)(CheckLogon *result))complete;
四. 關(guān)于UI布局
使用Interface Builder進行界面布局
Xib文件的命名與其對應(yīng)的.h文件保持相同
Xib文件中控件的組織結(jié)構(gòu)要合理,Xib文件中控件需要有合理的可讀性強的命名棒坏,方便他人理解
五. 格式化代碼
1> 指針 "" 位置
定義一個對象時燕差,指針 "" 靠近變量
例子: NSString *userName;
2> 方法的聲明和定義
在 - 、+ 和 返回值 之間留一個空格坝冕,方法名和第一個參數(shù)之間不留空格
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(NSBundle *)nibBundleOrNil
{...}
3> 代碼縮進
使用 xcode 默認縮進徒探,即 tab = 4空格
使用 xcode 中 re-indent 功能定期對代碼格式進行整理
相同類型變量聲明需要獨行聲明
例子:
CGFloatoringX = frame.origin.x;
CGFloatoringY = frame.origin.y;
CGFloatlineWidth = frame.size.width;
Method與Method之間空一行
例子:
#pragma mark - private methods
- (void)samplePrivateMethod
{...}
- (void)sampleForIf
{...}
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
{...}
具體 使用下面:
#pragma mark - ---- lefe cycle
viewdidLoad、viewWillAppear 等系統(tǒng)生命周期
#pragma mark - ---- event response
比如 button徽诲、tap刹帕、NSNotifaction 等 action 點擊方法
#pragma mark - ---- private methods
私有方法、 比如 建立 UI谎替、 初始化偷溺、等
#pragma mark - ---- delegate
放Delegate 等
#pragma mark - ---- getters & setters & public
5> 大括號寫法
對于類的method: 左括號另起一行寫(遵循蘋果官方文檔)
例子:
- (id)initWithNibName:(NSString *)nibNameOrNilbundle:(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) {
caseSampleEnumUndefined:{
// do something
break;
}
caseSampleEnumOne:{
// do something
break;
}
caseSampleEnumTwo:{
// do something
break;
}
default:{
NSLog(@"WARNING: there is an enum type not handled properly!");
break;
}
}
任何需要寫大括號的部分,不得省略
錯誤示例:
- (void)wrongExamples
{
BOOLsomeCondition = YES;
if (someCondition)
NSLog(@"this is wrong!!!");
while(someCondition)
NSLog(@"this is wrong!!!");
}