公司代碼規(guī)范(創(chuàng)業(yè)邦內(nèi)部使用)
命名基礎(chǔ)
在?面向?qū)ο筌浖斓脑O(shè)計過程中,開發(fā)人員經(jīng)常忽視對類,方法,函數(shù),常量以及其他編程接?元素的命名。本節(jié)討論大多數(shù)Cocoa
接?的一些命名約定。
前綴
所有類名、枚舉、結(jié)構(gòu)、protocol
定義時,都加上全大寫的CYB
作為前綴
后綴
所有protocol
定義時,都加上后綴Delegate
。如,CYBRefreshViewDelegate
,表示RefreshView
的協(xié)議
導入頭文件
對于Objective-C
的類文件,使用#import
導入;對于c,c++
文件,使用#include
導入
方法命名
方法名首字母小寫,其他單詞首字母大寫,每個空格分割的名稱以動詞開頭。如:
- (void)insertModel:(id)model atIndex:(NSUInteger)atIndex;
屬性、變量命名
每個屬性命名都加上類型后綴,如,按鈕就加上Button
后綴,模型就加上Model
后綴。eg:
@property (nonatomic, strong) UIButton *submitButton;
對于內(nèi)部私有變量,也需要加上后綴并以下劃線開頭,如:_titleLabel
, _descLabel
, _okButton
一般性原則
最好是既清晰?又簡短,但不要為簡短?而喪失清晰性
代碼 | 點評 |
---|---|
insertObject:atIndex: | Good |
insert:at: | 不清晰;要插?什么?“at”表?示什么? |
removeObjectAtIndex: | Good |
removeObject: | 不錯,因為?法是?用來移除作為參數(shù)的對象 |
remove: | 不清晰;要移除什么? |
名稱通常不縮寫,即使名稱很?,也要拼寫完全
代碼 | 點評 |
---|---|
destinationSelection | Good |
destSel | 不清晰 |
setBackgroundColor: | Good |
setBkgdColor: | 不清晰 |
一致性
盡可能與Cocoa
編程接?命名保持一致。如果你不太確定某個命名的?致性,請瀏覽頭文件或參考文檔中的范例,在使?多態(tài)方法的類中,命名的?致性?常重要。在不同類中實現(xiàn)相同功能的?法應(yīng)該具有同的名稱。
代碼 | 點評 |
---|---|
- (NSInteger)tag | 在 NSView, NSCell, NSControl 中有定義 |
- (void)setStringValue:(NSString *) | 在許多 Cocoa classes 中都有定義 |
訪問方法
訪問方法是對象屬性的讀取與設(shè)置?法。其命名有特定的格式,依賴于屬性的描述內(nèi)容。
- 如果屬性是用名詞表達的,則命名格式為:
- (type)noun;
- (void)setNoun:(type)aNoun;
例如:
- (NSString *)title;
- (void)setTitle:(NSString *)aTitle;
- 如果屬性是?形容詞表達的,則命名格式為:
- (BOOL)isAdjective;
- (void)setAdjective:(BOOL)isAdjective;
例如:
- (BOOL)isEditable;
- (void)setEditable:(BOOL)isEditable;
- 如果屬性是?動詞表達的,則命名格式為:
- (BOOL)verbObject;
- (void)setVerbObject:(BOOL)flag;
例如:
- (BOOL)showsAlpha;
- (void)setShowsAlpha:(BOOL)flag;
動詞要用現(xiàn)在時時態(tài)
- 不要使?用動詞的過去分詞形式作形容詞使?用
- 可以使?用情態(tài)動詞(
can
,should
,will
等)來提?高清晰性,但不要使?用do
或does
- (BOOL)openFile:(NSString *)fullPath withApplication:
(NSString *)appName andDeactivate:(BOOL)flag;
- (void)setAcceptsGlyphInfo:(BOOL)flag;
// 不好
- (BOOL)acceptsGlyphInfo;
- (void)setGlyphInfoAccepted:(BOOL)flag;
// 不好
- (BOOL)glyphInfoAccepted;
// 好
- (void)setCanHide:(BOOL)flag;
- (BOOL)canHide;
-
// 好
- (void)setShouldCloseDocument:(BOOL)flag;
// 不好
- (BOOL)shouldCloseDocument;
// 好
- (void)setDoesAcceptGlyphInfo:(BOOL)flag;
- (BOOL)doesAcceptGlyphInfo;
只有在方法需要間接返回多個值的情況下,才使?get
像上面這樣的方法,在其實現(xiàn)里應(yīng)允許接受NULL
作為其in/out
參數(shù),以表示調(diào)?者對?個或多個 返回值不感興趣。
委托方法
委托方法是那些在特定事件發(fā)?生時可被對象調(diào)?用,并聲明在對象的委托類中的方法。它們有獨特的命名約 定,這些命名約定同樣也適?用于對象的數(shù)據(jù)源方法。
- 名稱以標?發(fā)送消息的對象的類名開頭,省略類名的前綴并小寫第一個字母
-(BOOL)tableView:(NSTableView*)tableView shouldSelectRow:(int)row;
- (BOOL)application:(NSApplication *)sender openFile:(NSString
*)filename;
- 冒號緊跟在類名之后(隨后的那個參數(shù)表?委派的對象)。該規(guī)則不適用于只有?一個
sender
參數(shù) 的?法
- (BOOL)applicationOpenUntitledFile:(NSApplication *)sender;
- 上?的那條規(guī)則也不適?于響應(yīng)通知的?法。在這種情況下,方法的唯?參數(shù)表?通知對象
- (void)windowDidChangeScreen:(NSNotification *)notification;
- ?于通知委托對象操作即將發(fā)生或已經(jīng)發(fā)?的方法名中要使?
did
或will
- (void)browserDidScroll:(NSBrowser *)sender;
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window;
- ?用于詢問委托對象可否執(zhí)行某操作的?法名中可使?
did
或will
,但最好使?should
- (BOOL)windowShouldClose:(id)sender;
枚舉常量
- 聲明枚舉類型時,命名以
CYB
為前綴,而枚舉值以小寫k
開頭,后面的單詞首字母大寫,其余小寫。如:
typedef NS_ENUM(NSUInteger, CYBContentMode) {
kContentModeScaleFit = 1 << 1,
kContentModeScaleFill = 1 << 2
};
const常量
以小寫k
開頭,后面單詞首字母大寫,其余小寫。如:
const float kMaxHeigt = 100.0f;
如果是靜態(tài)常量,僅限本類內(nèi)使用的,加上前綴s_
,如果是整個工程共用,以sg_
為前綴。如:
s_kMaxHeight; sg_kMaxHeight;
其他常量
- 使用
#define
聲明普通常量,以小寫k
開頭,后面的單詞首字母大寫,其余小寫。如:
#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
- 通知常量名,以Notification為后綴,如:
#define kLoginSuccessNotification @”CYBLoginSucessNotification”
代碼注釋
類注釋
類頭需要有注釋,功能說明,作者等:如,
/**
* 這里是功能說明
*
* @author
* @modify 如果有修改需要這行,加上修改人和修改日期
*/
@interface CYBUIMaker : NSObject
方法注釋
方法注釋需要加上作者,功能說明,參數(shù)說明,返回值說明:
/**
* @author
* 描述內(nèi)容
*
* @param string <#string description#>
* @param font <#font description#>
*
* @return <#return value description#>
*/
+ (CGSize)sizeWithString:(NSString*)string andFont:(UIFont *)font;
塊注釋
對于塊注釋,有多種多種風格
風格一:
/////////////////////////////////////
// @name UIButton控件生成相關(guān)API
/////////////////////////////////////
風格二:
//
// 塊功能說明
//
風格三:
/*
* 塊功能說明
*
*/
類內(nèi)分塊結(jié)構(gòu)寫法
有生命周期的類,從上到下結(jié)構(gòu)為:
#pragma mark – life cycle
#pragma mark – Delegate
#pragma mark – CustomDelegate…
#pragma mark – Event
#pragma mark - Network
#pragma mark – Private
#pragma mark – Getter/Setter
參考
本文檔參考[Coding Guidelines for Cocoa](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ CodingGuidelines/CodingGuidelines.html)
import規(guī)范
當一個Controller或者一個Class中需要用到不同的類和Define時, 我們應(yīng)當把#import劃分.
劃分原則: 哪個Controller或者Class是本Controller或者Class的次級就放在一起, 公共Controller或Class就與之前的空一行, 緊跟著下面.
比如:
#import "AspManageServicePasswordViewController.h"
#import "AspResetServicePasswordViewController.h"
#import "AspServicePasswordView.h"
#import "NorNavShareView.h"
#import "AppDelegate.h"
#import "BCTabBarView.h"
#import "ModifyPwdViewController.h"
#import "AspLoginVC.h"
# Define規(guī)范
使用#define時, 必須要以數(shù)值對齊.
比如:
#define kLabelSize 20
#define kLabelMargins 20
#define kConfirmBottom 63
錯誤寫法:
#define kLabelSize 20
#define kLabelMargins 20
#define kConfirmBottom 63
使用#define定義預編譯宏時, 應(yīng)當把宏定義成全部大寫字母, 且與“_”分隔, 此方法是蘋果官方所推薦.
比如:
#define NS_AVAILABLE(_mac, _ios) CF_AVAILABLE(_mac, _ios)
#define NS_AVAILABLE_MAC(_mac) CF_AVAILABLE_MAC(_mac)
#define NS_AVAILABLE_IOS(_ios) CF_AVAILABLE_IOS(_ios)
錯誤寫法:
#define kCancelBottom 25
#define kCancelHeight 44
#define kCancelMargins 25
在代碼中盡量減少在代碼中直接使用數(shù)字常量, 而使用#define來進行聲明常量或運算.
比如:
#define kTableViewHeight 300
#define kTableViewCellHeight label.height + view.height
錯誤寫法:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.frame = CGRectMake(0, 64, 320, 660);
}
盡量代碼中得重復計算, 比如在代碼中很多地方需要使用到屏幕的寬度, 然后再進行計算, 我們就可以把屏幕的寬度進行抽取封裝成#define.
比如:
#define AspScreenWidth ([[UIScreen mainScreen] bounds].size.width)
#define kTableViewHeight 300
#define NAV_VIEW_HEIGHT 64
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.frame = CGRectMake(0, NAV_VIEW_HEIGHT, AspScreenWidth, kTableViewHeight);
}
錯誤寫法:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.frame = CGRectMake(0, 64, 320, 660);
}
paragma Mark 規(guī)范
使用#pragma mark時緊挨著方法名, 且與上一個方法的間距為1空行.
比如:
- (void)mineViewModel {
// Code Body
}
#pragma mark - TableViewRefresh
- (void)pulldownRefresh:(AspTableView *)tableView {
[self.mineViewModel requestDataWithSuccess:nil failure:nil];
}
錯誤寫法:
- (void)mineViewModel {
// Code Body
}
#pragma mark - TableViewRefresh
- (void)pulldownRefresh:(AspTableView *)tableView {
[self.mineViewModel requestDataWithSuccess:nil failure:nil];
}
- (void)mineViewModel {
// Code Body
}
#pragma mark - TableViewRefresh
- (void)pulldownRefresh:(AspTableView *)tableView {
[self.mineViewModel requestDataWithSuccess:nil failure:nil];
}
當兩個方法相近或者都是同一個模塊里的方法, 使用#pragma mark加注釋來導航代碼.
比如:
#pragma mark - Some Mothed
- (void)someMothedOne;
- (void)someMothedTwo;
- (void)someMothedThree;
錯誤寫法:
#pragma mark -
- (void)someMothedOne;
- (void)someMothedTwo;
- (void)someMothedThree;
- (void)someMothedOne;
- (void)someMothedTwo;
- (void)someMothedThree;
@interface規(guī)范
在@Interface里遵循的Delegate, 或者是DataSource方法, 必須以,間隔, 且空一格.
比如:
@interface AspEasyOwnResetPasswordViewController () <UITextFieldDelegate, MBAlertViewDelegate, UITableVIewDelegate, UITextViewDelegate, UIAlertViewDelegate, UITableViewDataSource>
錯誤寫法:
@interface AspEasyOwnResetPasswordViewController()<UITextFieldDelegate,MBAlertViewDelegate>
當我們在@interface里聲明變量時, 我們應(yīng)該根據(jù)內(nèi)存管理關(guān)鍵字來劃分模塊, 且以 “*” 分類對齊(基礎(chǔ)數(shù)據(jù)類型除外).
比如:
@interface AspResetServicePasswordViewController () <UITextFieldDelegate, MBAlertViewDelegate>
@property (nonatomic, strong) AspRetrievePasswordView *retrievePassword;
@property (nonatomic, strong) UITextField *foucsTextField;
@property (nonatomic, copy) NSString *brandName;
@property (nonatomic, copy) NSString *brandType;
@property (nonatomic, assign) NSTimer *timer;
@property (nonatomic, assign) NSInteger zeroCount;
@property (nonatomic, assign) NSInteger verificationCode;
@end
錯誤寫法:
@property (nonatomic, strong) NSDictionary *everyDataDic;
@property (nonatomic, copy) NSString *timeStr;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) SHLineGraphView *lineGraph;
在@interface里聲明@property需要把nonatomic放在前(使用xib的除外), strong等內(nèi)存關(guān)鍵字放在后, 且“()”前后各空一格.
比如:
@property (nonatomic, strong) UILabel *unitLabel;
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) NSMutableArray *dayDataObjects;
錯誤寫法:
@property ( nonatomic, retain ) UIScrollView* navScrollView;
@property ( nonatomic, retain ) NSMutableArray* ItemBtns;
@implementation規(guī)范
禁止使用@synthesize
關(guān)鍵字, @property
已經(jīng)自動幫我們聲明了Setter和Getter方法, 如有特殊需求可重寫Setter方法, 所以不需要再使用@synthesize
.
錯誤寫法:
@implementation FreeExperienceView
@synthesize segment = _segment;
@synthesize dataInfo = _dataInfo;
@synthesize delegate = _delegate;
@synthesize MyEXPView = _MyEXPView
@synthesize canEXPView = _canEXPView;
@synthesize isRefresh = _isRefresh;
實例規(guī)范
一個實例變量出現(xiàn)屬性賦值時, 如果是以“=”賦值, 必須以“=”對齊, 如果以“[]”賦值, 必須以開頭第一個“[”對齊.
比如:
- (void)setTextFieldDelegate:(id<UITextFieldDelegate>)textFieldDelegate {
self.phoneTextFieldOne.delegate = textFieldDelegate;
self.phoneTextFieldTwo.delegate = textFieldDelegate;
self.phoneTextFieldThree.delegate = textFieldDelegate;
self.reviewPassword.delegate = textFieldDelegate;
self.confirmPassword.delegate = textFieldDelegate;
}
錯誤寫法:
- (void)setTextFieldDelegate:(id<UITextFieldDelegate>)textFieldDelegate {
self.phoneTextFieldOne.delegate = textFieldDelegate;
self.phoneTextFieldTwo.delegate = textFieldDelegate;
self.phoneTextFieldThree.delegate = textFieldDelegate;
self.reviewPassword.delegate = textFieldDelegate;
self.confirmPassword.delegate = textFieldDelegate;
}
當出現(xiàn)多個實例變量時, 應(yīng)該使用代碼塊的思想, 把它們組成一個模塊一個模塊(特殊情況除外)比如需要局部聲明的AVFoundation.
比如:
- (void)customFunction {
Person *person = [[Person alloc] init];
person.name = @"xiaoming";
person.age = 17;
Car *newCar = [[Car alloc] init];
newCar.name = @"保時捷";
}
錯誤寫法:
- (void)customFunction {
Person *person = [[Person alloc] init];
person.name = @"xiaoming";
person.age = 17;
Car *newCar = [[Car alloc] init];
newCar.name = @"保時捷";
}
禁止在實例對象, 賦值, 調(diào)用方法后的“;”與前面代碼空格.
比如:
- (void)customFunction:(NSString *)someName {
self.userName = someName;
}
錯誤寫法:
- (void)customFunction:(NSString *)someName {
self.userName = someName ;
}
在制類型轉(zhuǎn)換時, 函數(shù)強轉(zhuǎn)類型與被強轉(zhuǎn)函數(shù)名之間不放置空格, 但類型中的Type與*必須空格.
比如:
NSString *userName = (NSString *)student;
錯誤寫法:
NSString *userName = (NSString *) student;
NSDictionary規(guī)范
當NSDictionary里的Key : Value數(shù)量大于或者等于2時, 應(yīng)拆分為多行顯示, 并以 ":" 對齊.
比如:
NSDictionary *people = @{@"xiaoming" : 18,
@"xiaohong" : 19,
@"xiaowang" : 20};
錯誤寫法:
NSDictionary *people = @{@"xiaoming":18,@"xiaohong":19,@"xiaowang":20};
NSArray規(guī)范
當NSArray里的元素數(shù)量大于或者等于2時, 應(yīng)拆分為多行顯示, 并以第一個元素對齊.
比如:
NSArray *nameArray = @[@"小明",
@"小紅,
@"小王"];
錯誤寫法:
NSArray *nameArray = @[@"小明",@"小紅",@"小王"];
函數(shù)規(guī)范
在聲明函數(shù)時“-”與“(type)”必須空一格, 且后面的參數(shù)類型與“*”也必須空一格.
比如:
- (void)customFunction {
// Code Body
}
錯誤寫法:
-(void)customFunction {
// Code Body
}
-(void) customFunction {
// Code Body
}
方法與方法之間相隔一行的間距, 禁止不空行或多空行.
比如:
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//Code Body
}
return self;
}
- (void)customFunction {
//Code Body
}
錯誤寫法:
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//Code Body
}
return self;
}
- (void)customFunction {
//Code Body
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
//Code Body
}
return self;
}
- (void)customFunction {
//Code Body
}
在每個方法定義前需要留白一行, 也就是每個方法之間留空一行, 如果是系統(tǒng)的方法則可以不留空.
比如:
- (void)viewDidLoad {
[super viewDidLoad];
// Code Body
}
錯誤寫法:
- (void)viewDidLoad {
[super viewDidLoad];
// Code Body
}
- (void)customFunction {
[self customFunction];
// Code Body
}
在函數(shù)體內(nèi), 如果第一句調(diào)用的是系統(tǒng)方法, 可不換行, 如果是自定義的方法(包括if-else和for-in), 必須換行.
比如:
- (void)mineViewModel {
[self customFunction]
}
錯誤寫法:
- (void)mineViewModel {
[self customFunction]
}
在函數(shù)體里, 如果只有一句代碼, 可與函數(shù)名緊挨
比如:
- (NSString *)iconImageName {
return @"icon_nav5";
}
錯誤寫法:
- (NSString *)iconImageName {
return @"icon_nav5";
}
任意行代碼不能超過80個字符, 如果超過80個字符, 可以考慮多行顯示, 比如有多個參數(shù)時, 可以每個參數(shù)放一行.
比如:
- (void)replaceObjectsInRange:(NSRange)range
withObjectsFromArray:(NSArray<ObjectType> *)otherArray
range:(NSRange)otherRange;
錯誤寫法:
- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray<ObjectType> *)otherArray range:(NSRange)otherRange;
如果實現(xiàn)的是空函數(shù)體, 那么就不需要空格.
比如:
- (void)showView {}
錯誤寫法:
- (void)showView {
}
- (void)showView {
}
if-else規(guī)范
如果在方法中出現(xiàn)if-else判斷, 我們應(yīng)該使用結(jié)構(gòu)清晰的排版方式, if-else語句與方法名空行, 與return語句可以不用空行.
比如:
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// code body
}
return self;
}
錯誤寫法:
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// code body
}
return self;
}
if-else內(nèi)部也必須跟著空行, 在else之后的"}"可不空行.
比如:
- (void)viewDidLoad {
[super viewDidLoad];
if (age < 0) {
// Code Body
}
}
錯誤寫法:
- (void)viewDidLoad {
[super viewDidLoad];
if (age < 0) {
// Code Body
}
}
if-else超過四層的時候, 就要考慮重構(gòu), 多層的if-else結(jié)構(gòu)很難維護.
比如:
- (void)viewDidLoad {
[super viewDidLoad];
if (condition) {
// Code Body
} else if (condition){
// Code Body
} else if (condition){
// Code Body
} else if (condition){
// Code Body
} else if (condition){
// Code Body
} else if (condition){
// Code Body
} else {
// Code Body
}
}
合理使用if-else, 當某個場景只需滿足一個條件才能進入if-else時, 應(yīng)該把if-else抽成一個模塊.
比如:
- (void)viewDidLoad {
[super viewDidLoad];
if (age < 0) {
NSLog(@"這是錯誤的年齡");
return;
}
NSLog(@"現(xiàn)在時%ld歲", (long)age):
}
錯誤寫法:
- (void)viewDidLoad {
[super viewDidLoad];
if (age < 0) {
NSLog(@"這是錯誤的年齡");
} else {
NSLog(@"現(xiàn)在時%ld歲", (long)age):
}
}
在使用if-else時必須加"{}"符號, 如果存在else時, 必須加{}
比如:
- (void)customFunction {
if (condition) {
// Code Body
} else {
// Code Body
}
}
錯誤寫法:
- (void)customFunction {
if (condition) // Code Body
else // Code Body
}
- (void)customFunction {
if (condition) // Code Body
// Code Body
}
在使用if-else中, ()中的條件, 不需要與左右兩邊的()空格, 且{}與前一個)空格
比如:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self){
[self setBackgroundColor:TRAFFICDETAILBACGROUNDCOLOR];
if (!_ItemBtns) {
_ItemBtns = [[NSMutableArray alloc] init];
}
_mnScrollViewWidth = kWidthToFit(94);
self.colorNoSel = UIColorFromRGB(0xdee6ea);
self.colorSel = UIColorFromRGB(0xcdd0d2);
[self addSubview:self.navScrollView];
}
return self;
}
錯誤寫法
- (id) initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if ( self )
{
[self setBackgroundColor:TRAFFICDETAILBACGROUNDCOLOR];
if ( !_ItemBtns ) {
_ItemBtns = [[NSMutableArray alloc] init] ;
}
_mnScrollViewWidth = kWidthToFit(94);
self.colorNoSel = UIColorFromRGB(0xdee6ea) ;
self.colorSel = UIColorFromRGB(0xcdd0d2) ;
[self addSubview:self.navScrollView];
}
return self;
}
For-In & For 規(guī)范
當函數(shù)中需要使用到"for(for-in)", 在"for(for-in)"內(nèi), 必須和上文的"if-else"一樣空行, 與return語句可不空行.
比如:
- (void)customFunction {
for (NSInteger i = 0; i < 10; i++) {
// code body
}
}
錯誤寫法:
- (void)customFunction {
for (NSInteger i = 0; i < 10; i++) {
// code body
}
}
在"for(for-in)"中, 里面的參數(shù)必須嚴格按照上文所示的實例規(guī)范來聲明, 且是兼容64位的類型.
比如:
- (void)viewDidLoad {
[super viewDidLoad];
for (NSInteger i = 0; i < 10; i++) {
// code body
}
}
錯誤寫法:
- (void)viewDidLoad {
[super viewDidLoad];
for (NSInteger i=0;i<10;i++) {
// code body
}
}
當函數(shù)中需要使用到"for(for-in)", 如果該"for(for-in)"是在函數(shù)的第一句, 必須和上文的"if-else"一樣空行, 與return語句可不空行.
比如:
- (void)customFunction {
for (NSInteger i = 0; i < 10; i++) {
// code body
}
}
錯誤寫法:
- (void)customFunction {
for (NSInteger i = 0; i < 10; i++) {
// code body
}
}
Block規(guī)范
在函數(shù)中使用到Block時, 與if-else或者for-in不太一樣, Block第一行與代碼塊必須得空行, 無論方法是否是系統(tǒng)自帶的.
比如:
- (void)customFunction {
[className blockName:^(parameter) {
// Code Body
}];
}
錯誤寫法:
- (void)customFunction {
[className blockName:^(parameter) {
// Code Body
}];
}
運算符規(guī)范
一元運算符和參數(shù)之間不放置空格, 比如"!"非運算符, "&"安位與, "|"安位或.
比如:
BOOL isOpen = YES;
BOOL isClose = !isOpen;
錯誤寫法:
BOOL isOpen = YES;
BOOL isClose = ! isOpen;
BOOL isOpen=YES;
BOOL isClose=!isOpen;
二元運算符和參數(shù)之間要有空格, 如賦值號"="左右各留一個空格.
比如:
self.myString = @“mySring”;
錯誤寫法:
self.myString=@"myString";
三目運算符和參數(shù)之間必須得有空格, 如判斷符"?"結(jié)果符":"
比如:
NSInteger userAge = @"Man" ? 18 : 19;
錯誤寫法:
NSInteger userAge = @"Man"?18:19;
命名規(guī)范
實例命名規(guī)范
使用完整的英文描述來準確描述Variable /Class /Define /Notes, 禁止使用拼音命名, 不建議建議使用縮寫.
比如:
NSString *userName = @"小明";
錯誤寫法:
NSString *uName = @"小明";
使用本行業(yè)的專業(yè)術(shù)語, 如果是消費者的話就要使用消費者對應(yīng)的名詞, 用戶的話就使用用戶對應(yīng)的名詞, 并且以“=”對齊, 禁止夸域命名.
比如:
NSString *userName = @"小明";
NSInteger userAge = 18;
CGFloat userHeight = 170.0f;
錯誤寫法:
NSString *uName = @"小明";
NSInteger uAge = 18;
CGFloat uHeight = 170.5f;
使用Apple推薦的駝峰命名方式來編寫代碼, 方法開頭第一個字母必須小寫(特殊命名除外), 禁止使用隨性的命名方式.
比如:
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
錯誤寫法:
- (CGPoint)ConverTPoint:(CGPoint)POINT ToView:(nullable UIView *)view;
@Property命名規(guī)范
盡量少用縮寫, 使用Apple推薦的短句命名法命名且第一個字母必須小寫, 這樣子做, 可以省略再注釋處理.
比如:
@property (readonly, copy) NSString *decomposedStringWithCanonicalMapping;
@property (readonly, copy) NSString *precomposedStringWithCanonicalMapping;
@property (readonly, copy) NSString *decomposedStringWithCompatibilityMapping;
@property (readonly, copy) NSString *precomposedStringWithCompatibilityMapping;
錯誤寫法:
@property (readonly, copy) NSString *uName;
@property (readonly, assign) NSInteger *uAge;
@property (readonly, assign) CGFloat *uHeight;
@Interface->class命名規(guī)范
盡量使用有意義的英文名字命名, 拒絕使用"i", "j"等無意義的字符命名, 類的命名首字母大寫, 其他變量的命名首字符小寫.
比如:
@interface People : SuperClassName
@end
錯誤寫法:
@interface people : SuperClassName
@end
@interface ren : SuperClassName
@end
Block命名規(guī)范
盡量使用有意義的命名方法, 禁止使用簡稱去命名.
比如:
@property (nonatomic, copy) void(^aspWebNavigationRightBtnBlock)(parameter);
錯誤寫法:
@property (nonatomic, copy) void(^aspWNavRBBlock)(parameter);
for-in命名規(guī)范
在使用快速遍歷for-in時, 其中的參數(shù)需要根據(jù)對應(yīng)的元素進行一一對應(yīng)來命名.
比如:
NSArray *numberArray = @[@1, @2, @3, @4, @5 , @6, @7, @8, @9];
for (id number in numberArray) {
NSLog(@"%@", number);
}
錯誤寫法:
NSArray *numberArray = @[@1, @2, @3, @4, @5 , @6, @7, @8, @9];
for (id item in numberArray) {
NSLog(@"%@", item);
}
NSArray *numberArray = @[@1, @2, @3, @4, @5 , @6, @7, @8, @9];
for (id abc in numberArray) {
NSLog(@"%@", item);
}
注意: 在工程中少使用Xib, 禁止在StoryBoard拖入任何控件, 且StoryBoard只可作為項目架構(gòu)所使用. 文件夾層次結(jié)構(gòu)