簡(jiǎn)單-項(xiàng)目開發(fā)步驟之代碼規(guī)范

1.建議用.符號(hào)代替括號(hào)用于設(shè)置或獲取屬性值

For example:

view.backgroundColor = [UIColororangeColor];[UIApplicationsharedApplication].delegate;

Not:

[viewsetBackgroundColor:[UIColororangeColor]];

UIApplication.sharedApplication.delegate;

2.空格

a.縮進(jìn)必須使用4個(gè)空格。沒有縮進(jìn)和制表符怖现。

b.一定要在Xcode中設(shè)置此首選項(xiàng)基矮。方法括號(hào)和其他括號(hào)(如果/其他/開關(guān)/等等)盟萨。必須打開在同一行語(yǔ)句。括號(hào)必須關(guān)閉在一個(gè)新行徙垫。

For example:

if(user.isHappy)?{

//?Do?something

}

else{

//?Do?something?else

}

3.條件語(yǔ)句:必須有括號(hào)

For example:

if(!error) {returnsuccess;}

Not:

if(!error)returnsuccess;

or

if(!error)returnsuccess;

4.三元運(yùn)算符:三元操作符的意圖,?,是增加清晰或代碼整潔腾它。三元應(yīng)該只評(píng)估每一個(gè)條件表達(dá)式乒裆。評(píng)估多個(gè)條件通常是更容易理解作為一個(gè)if語(yǔ)句或重構(gòu)命名變量璃俗。

For example:

result = a > b ? x : y;

Not:

result?=?a?>?b???x?=?c?>?d???c?:?d?:?y;

5.錯(cuò)誤處理:當(dāng)方法返回一個(gè)錯(cuò)誤參數(shù)通過(guò)引用,代碼必須打開返回值,不得打開錯(cuò)誤變量奴璃。

For example:

NSError*error;if(![selftrySomethingWithError:&error]) {// Handle Error}

Not:

NSError*error;[selftrySomethingWithError:&error];if(error) {// Handle Error}

6.方法

For example:

-?(void)setExampleText:(NSString*)text?image:(UIImage?*)image;

7.變量:見名知意

For example:

NSString *title: It is reasonable to assume a “title” is a string.

NSString *titleHTML: This indicates a title that may contain HTML which needs parsing for display.“HTML” is needed for a programmer to use this variable effectively.

NSAttributedString *titleAttributedString: A title, already formatted for display.AttributedStringhints that this value is not just a vanilla title, and adding it could be a reasonable choice depending on context.

NSDate *now:No further clarification is needed.

NSDate *lastModifiedDate: SimplylastModifiedcan be ambiguous; depending on context, one could reasonably assume it is one of a few different types.

NSURL *URLvs.NSString *URLString: In situations when a value can reasonably be represented by different classes, it is often useful to disambiguate in the variable’s name.

NSString *releaseDateString: Another example where a value could be represented by another class, and the name can help disambiguate.

Single letter variable names are NOT RECOMMENDED, except as simple counter variables in loops.

Asterisks indicating a type is a pointer MUST be “attached to” the variable name.For example,NSString *textnotNSString* textorNSString * text, except in the case of constants (NSString * const NYTConstantString).

For example:

interfaceNYTSection:NSObject@property(nonatomic)NSString*headline;@end

Not:

@interfaceNYTSection:NSObject{NSString*headline;}

8.變量限定符:

For example:

NSString * __weak text.

9.命名:描述性的方法和變量名越清楚越好

For example:

UIButton *settingsButton;

Not

UIButton *setBut;

For example:

staticconstNSTimeIntervalNYTArticleViewControllerNavigationFadeAnimationDuration =0.3;

Not:

staticconstNSTimeIntervalfadetime =1.7;

For example:

@synthesize descriptiveVariableName = _descriptiveVariableName;

Not:

idvarnm;

10.分類

類別建議簡(jiǎn)明地細(xì)分功能,用來(lái)描述該功能

For example:

@interfaceUIViewController(NYTMediaPlaying)@interfaceNSString(NSStringEncodingDetection)

Not:

@interfaceNYTAdvertisement(private)

@interfaceNSString(NYTAdditions)

For example:

@interfaceNSArray(NYTAccessors)- (id)nyt_objectOrNilAtIndex:(NSUInteger)index;@end

Not:

@interfaceNSArray(NYTAccessors)

-?(id)objectOrNilAtIndex:(NSUInteger)index;

@end

11.注釋

When they are needed, comments SHOULD be used to explainwhya particular piece of code does something. Any comments that are used MUST be kept up-to-date or deleted.

Block comments are NOT RECOMMENDED, as code should be as self-documenting as possible, with only the need for intermittent, few-line explanations. This does not apply to those comments used to generate documentation.

12.init and dealloc

dealloc方法應(yīng)放置在頂部的實(shí)現(xiàn),直接放在@ synthesize和@dynamic語(yǔ)句之后。init方法應(yīng)該直接放置在dealloc任何類的方法城豁。

-?(instancetype)init?{

self?=?[superinit];//?or?call?the?designated?initializer

if(self)?{

//?Custom?initialization

}

returnself;

}

12.文字

For example:

NSArray*names = @[@"Brian",@"Matt",@"Chris",@"Alex",@"Steve",@"Paul"];NSDictionary*productManagers = @{@"iPhone":@"Kate",@"iPad":@"Kamal",@"Mobile Web":@"Bill"};NSNumber*shouldUseLiterals = @YES;NSNumber*buildingZIPCode = @10018;

Not:

NSArray*names?=?[NSArrayarrayWithObjects:@"Brian",@"Matt",@"Chris",@"Alex",@"Steve",@"Paul",nil];

NSDictionary*productManagers?=?[NSDictionarydictionaryWithObjectsAndKeys:@"Kate",@"iPhone",@"Kamal",@"iPad",@"Bill",@"Mobile?Web",nil];

NSNumber*shouldUseLiterals?=?[NSNumbernumberWithBool:YES];

NSNumber*buildingZIPCode?=?[NSNumbernumberWithInteger:10018];

13.CGRECT

For example:

CGRectframe = self.view.frame;CGFloatx = CGRectGetMinX(frame);CGFloaty = CGRectGetMinY(frame);CGFloatwidth = CGRectGetWidth(frame);CGFloatheight = CGRectGetHeight(frame);

Not:

CGRectframe?=?self.view.frame;

CGFloatx?=?frame.origin.x;

CGFloaty?=?frame.origin.y;

CGFloatwidth?=?frame.size.width;

CGFloatheight?=?frame.size.height;

14.常量

For example:

staticNSString*constNYTAboutViewControllerCompanyName =@"The New York Times Company";staticconstCGFloatNYTImageThumbnailHeight =50.0;

Not:

#defineCompanyName@"The?New?York?Times?Company"

#definethumbnailHeight2

15.枚舉

Example:

typedefNS_ENUM(NSInteger,?NYTAdRequestState)?{

NYTAdRequestStateInactive,

NYTAdRequestStateLoading

};

16.在處理位掩碼時(shí),必須使用NS_OPTIONS宏苟穆。

Example:

typedefNS_OPTIONS(NSUInteger,?NYTAdCategory)?{

NYTAdCategoryAutos? ? ? =1<<0,

NYTAdCategoryJobs? ? ? ?=1<<1,

NYTAdCategoryRealState? =1<<2,

NYTAdCategoryTechnology?=1<<3

};

17.私有方法:

Private properties SHALL be declared in class extensions (anonymous categories) in the implementation file of a class.

For example:

@interfaceNYTAdvertisement()

@property(nonatomic,strong)?GADBannerView?*googleAdView;

@property(nonatomic,strong)?ADBannerView?*iAdView;

@property(nonatomic,strong)?UIWebView?*adXWebView;

@end

18.圖片名稱

圖片應(yīng)該命名為一個(gè)駝峰式大小寫字符串的描述他們的目的,其次是無(wú)前綴的名稱他們定制的類或?qū)傩?如果有的話),其次是進(jìn)一步描述顏色和/或位置,最后他們的狀態(tài)。

For example:

RefreshBarButtonItem/RefreshBarButtonItem@2xandRefreshBarButtonItemSelected/RefreshBarButtonItemSelected@2x

ArticleNavigationBarWhite/ArticleNavigationBarWhite@2xandArticleNavigationBarBlackSelected/ArticleNavigationBarBlackSelected@2x.

18.boolen

Values MUST NOT be compared directly toYES, becauseYESis defined as1, and aBOOLin Objective-C is aCHARtype that is 8 bits long (so a value of11111110will returnNOif compared toYES).

For an object pointer:

if(!someObject) {}if(someObject ==nil) {}

For aBOOLvalue:

if(isAwesome)if(!someNumber.boolValue)if(someNumber.boolValue ==NO)

Not:

if(isAwesome ==YES)// Never do this.

@property?(assign,?getter=isEditable)BOOLeditable;

20.單例

Singleton objects SHOULD use a thread-safe pattern for creating their shared instance.

+?(instancetype)sharedInstance?{

staticidsharedInstance?=nil;

staticdispatch_once_tonceToken;

dispatch_once(&onceToken,?^{

sharedInstance?=?[[[selfclass]alloc]init];

});

returnsharedInstance;

}

21.imports

If there is more than one import statement, statements MUST be groupedtogether. Groups MAY be commented.

Note: For modules use the@importsyntax.

//?Frameworks

@import?QuartzCore;

//?Models

#import"NYTUser.h"

//?Views

#import"NYTButton.h"

#import"NYTUserView.h"

22.協(xié)議

In adelegate or data source protocol, the first parameter to each method SHOULD be the object sending the message.

This helps disambiguate in cases when an object is the delegate for multiple similarly-typed objects, and it helps clarify intent to readers of a class implementing these delegate methods.

For example:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath;

Not:

-?(void)didSelectTableRowAtIndexPath:(NSIndexPath*)indexPath;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末唱星,一起剝皮案震驚了整個(gè)濱河市雳旅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌间聊,老刑警劉巖攒盈,帶你破解...
    沈念sama閱讀 206,378評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異哎榴,居然都是意外死亡型豁,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門尚蝌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)迎变,“玉大人,你說(shuō)我怎么就攤上這事驼壶∈贤悖” “怎么了?”我有些...
    開封第一講書人閱讀 152,702評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵热凹,是天一觀的道長(zhǎng)泵喘。 經(jīng)常有香客問我,道長(zhǎng)般妙,這世上最難降的妖魔是什么纪铺? 我笑而不...
    開封第一講書人閱讀 55,259評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮碟渺,結(jié)果婚禮上鲜锚,老公的妹妹穿的比我還像新娘。我一直安慰自己苫拍,他們只是感情好芜繁,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評(píng)論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著绒极,像睡著了一般骏令。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上垄提,一...
    開封第一講書人閱讀 49,036評(píng)論 1 285
  • 那天榔袋,我揣著相機(jī)與錄音周拐,去河邊找鬼。 笑死凰兑,一個(gè)胖子當(dāng)著我的面吹牛妥粟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吏够,決...
    沈念sama閱讀 38,349評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼勾给,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了稿饰?” 一聲冷哼從身側(cè)響起锦秒,我...
    開封第一講書人閱讀 36,979評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎喉镰,沒想到半個(gè)月后旅择,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,469評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡侣姆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評(píng)論 2 323
  • 正文 我和宋清朗相戀三年生真,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捺宗。...
    茶點(diǎn)故事閱讀 38,059評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡柱蟀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蚜厉,到底是詐尸還是另有隱情长已,我是刑警寧澤,帶...
    沈念sama閱讀 33,703評(píng)論 4 323
  • 正文 年R本政府宣布昼牛,位于F島的核電站术瓮,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏贰健。R本人自食惡果不足惜胞四,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望伶椿。 院中可真熱鬧辜伟,春花似錦、人聲如沸脊另。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,262評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)偎痛。三九已至烘豌,卻和暖如春寺董,著一層夾襖步出監(jiān)牢的瞬間挣惰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工贺拣, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留靖榕,地道東北人标锄。 一個(gè)月前我還...
    沈念sama閱讀 45,501評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像茁计,于是被迫代替她去往敵國(guó)和親料皇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容