天天看點

Cocoa命名規範

類的命名

  • 類的命名需要加字首, 微網誌項目統一字首為WB. 如: WBMessageManager. 例外情況: 當某一子產品屬于比較獨立的組建時, 可以考慮使用該子產品的名字作為字首, 而不一定需要使用整個項目的字首. 比如Foundation的字首為NS, UIKit的字首為UI, Core Location的字首為CL等. 
  • 類的名字應該為名詞. 
  • 類的名字中每個單詞應以大寫字母開頭. 

接口的命名

  • 如果接口和某個類有關, 如作為某個類的Delegate, 則命名為 類的名 + Delegate. 如: WBMessageManagerDelegate. 
  • 接口的命名也需要加入統一字首. 
  • 接口的名字中每個單詞應以大寫字母開頭. 

方法的命名

基本規則

  • 方法名以小寫字母開頭, 其後每個單詞以大寫字母開頭(camelCase). 例外: 如果方法名以常用縮寫開頭, 可以用大寫字母開頭, 如(TIFFRepresentation). 
  • 方法如果是一個object的動作的話, 應該以動詞開頭. 但不要使用“do”或者“does”作為動詞的一部分, 而且不要在動詞前加副詞或者形容詞.

    - (void)invokeWithTarget:(id)target;

  • - (void)selectTabViewItem:(NSTabViewItem *)tabViewItem
  • 如果該方法是傳回一個屬性的值, 不要使用“get”開頭. 即使該值是現計算出來的, 也不要使用“compute”或者“calc”等詞.

    - (UIImage *)thumbnailImage;                    // Right

  • - (UIImage *)generateThumbnailImage;            // Wrong.
  • 盡量使參數之前的單詞去描述參數本身.

    - (id)viewWithTag:(int)aTag;                    // Right.

  • - (id)taggedView:(int)aTag;                     // Wrong.
  • 不要在方法中用“and”連接配接各各參數.

    - (int)runModalForDirectory:(NSString *)path 

  •                       file:(NSString *)name 
  •                       types:(NSArray *)fileTypes;          // Right
  • - (int)runModalForDirectory:(NSString *)path 
  •                     andFile:(NSString *)name 
  •                     andTypes:(NSArray *)fileTypes;          // Wrong
  • 但是如果是連接配接兩個動作, 則可以使用“and”.

    - (BOOL)openFile:(NSString *)fullPath withApplication:(NSString *)appName andDeactivate:(BOOL)flag;

屬性命名

  • 如果屬性是用名詞表示的, 格式如下:

    - (void)setNoun:(type)aNoun;

  • - (type)noun;
  • 比如:

    - (void)setEditable:(BOOL)flag;

  • - (BOOL)isEditable;
  • 如果屬性是用動詞表示的, 格式如下:

    - (void)setVerbObject:(BOOL)flag;

  • - (BOOL)verbObject;
  • 比如:

    - (void)setShowsAlpha:(BOOL)flag;

  • - (BOOL)showsAlpha;
  • 注意, 這裡動詞使用一般現在時. 
  • 不要用過去時去把一個動詞變成形容詞.

    - (void)setAcceptsGlyphInfo:(BOOL)flag;                     // Right

  • - (BOOL)acceptsGlyphInfo;                                   // Right
  • - (void)setGlyphInfoAccepted:(BOOL)flag;                    // Wrong
  • - (BOOL)glyphInfoAccepted;                                  // Wrong
  • 也可以使用“can”, “should”, “will”等詞去表示更明确的意義, 但是不要使用“do”或“does”.

    - (void)setCanHide:(BOOL)flag;                              // Right

  • - (BOOL)canHide;                                            // Right
  • - (void)setShouldCloseDocument:(BOOL)flag;                  // Right
  • - (BOOL)shouldCloseDocument;                                // Right
  • - (void)setDoesAcceptGlyphInfo:(BOOL)flag;                  // Wrong
  • - (BOOL)doesAcceptGlyphInfo;                                // Wrong
  • 僅當方法間接傳回值的時候使用“get”. 這種情形隻是用于有多個值需要傳回.

    - (void)getLineDash:(float *)pattern count:(int *)count phase:(float *)phase;

  • 注意: 在這樣的方法中需要支援傳入NULL, 以便不需要傳回某些資料. 

Delegate方法命名

  • 以發送該消息的類為開始.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

  • 如果沒有其他參數, 則采用以下類似的方式.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView;

  • 接受到的Notification方法的唯一參數為這個Notification類.

    - (void)keyboardWillHide:(NSNotification *)notification;

  • 使用“did”或“will”表示已經發生或即将發生的事件.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView;

  • - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
  • 使用“should”表示詢問是否觸發某種事件.

    - (BOOL)textFieldShouldReturn:(UITextField *)textField;

類方法命名

  • 以便捷初始化為目的的類方法通常以傳回的資料類型命名, 傳回的資料需要autorelease.

    + (id)statusWithDictionary:(NSDictionary *)dict;

  • + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
  • 其他類方法的命名參考以上内容. 

函數的命名

  • 函數名通常為以下幾種格式.

    Prefix + Value () 

  • Prefix + Value + With/From/For + Input () 
  • Prefix + Action () 
  • Prefix + Action + Type ()
  • 函數也需要加入統一字首以避免命名沖突. 函數名需要标明傳回資料的類型.

    NSHomeDirectory()

  • NSHomeDirectoryForUser()
  • NSClassFromString()
  • NSBeginAlertSheet()
  • NSDrawGrayBezel()

變量和資料類型的命名

變量的命名

  • 成員變量不以下劃線開頭.

    NSString *name;             // Right.

  • NSString *_name;            // Wrong.
  • 變量以小寫字母開頭, 其後每個單詞的首字母大寫.

    NSString *streetAddress     = @"1 Infinite Loop";

  • NSString *cityName          = @"Cupertino";
  • 如果變量的類型為常用類型, 如NSString, NSArray, NSNumber, BOOL等, 不将類型名加載變量名最後.

    NSString *accountName;              // Right.

  • NSString *accountNameString;        // Not good.
  • NSArray *mailboxes;                 // Right.
  • NSArray *mailboxesArray;            // Not good.
  • BOOL userInputWasUpdated;           // Right.
  • BOOL userInputWasUpdatedBOOL;       // Not good.
  • 如果變量不是以上類型 (通常為控件), 則需要在名字的末尾顯示的标明他們的類型.

    UIImage *defaultImage;

  • UIActivityIndicatorView *activityIndicatorView;
  • UILabel *timeLabel;
  • UIButton *editButton;
  • 如果變量表示的一個類型通常使用單例, 其類名去掉字首後可以直接使用為變量名.

    NSFileManager *fileManager;

  • 一般情況下, 使用複數表示該變量的類型是NSArray或NSSet.

    NSDictionary可以使用以下的命名方式.

    NSDictionary *keyedAccountNames;

  • NSDictionary *messageDictionary;
  • NSDictionary *messageDict;

常量

  • 使用枚舉值表示一組相關的整數常量, 枚舉常量的命名和函數命名一樣.

    typedef enum {

  •     UIImageOrientationUp,               // default orientation
  •     UIImageOrientationDown,             // 180 deg rotation
  •     UIImageOrientationLeft,             // 90 deg CCW
  •     UIImageOrientationRight,            // 90 deg CW
  •     UIImageOrientationUpMirrored,       // as above but image mirrored along other axis. horizontal flip
  •     UIImageOrientationDownMirrored,     // horizontal flip
  •     UIImageOrientationLeftMirrored,     // vertical flip
  •     UIImageOrientationRightMirrored,    // vertical flip
  • } UIImageOrientation;
  • 使用const定義類常量.

    static NSString *const WBVersionNumber = @"1.0.0";

  • static UIColor *const WBDefaultTextColor = [UIColor whiteColor];
  • 使用#define定義原始類型.

    #define WBDefaultTextSize           12

  • #define WBCellTopPadding            15.0

其他類型

  • 預處理字元使用大寫字母.

    #ifdef DEBUG

  • 使用extern定義通知或是字典鍵字元串常量.

    extern NSString *const UIApplicationWillTerminateNotification;

  • 其真實的字元串值在.m檔案中指派.

    NSString *const UIApplicationWillTerminateNotification = @"UIApplicationWillTerminateNotification";

  • 通知的命名規則.

    [Name of associated class] + [Did | Will] + [UniquePartOfName] + Notification

  • 如:

    UIApplicationWillEnterForegroundNotification

  • UIApplicationDidEnterBackgroundNotification

常用縮寫表

  • ASCII, PDF, XML, HTML, URL, RTF, HTTP, TIFF, JPG, GIF, LZW, ROM, RGB, CMYK, MIDI, FTP, etc.
iOS
下一篇: cocoa 繪圖