聲明:git
文件註釋:採用Xcode自動生成的註釋格式。segmentfault
// // AppDelegate.h // 項目名稱 // // Created by 開發者姓名 on 2018/6/8. // Copyright © 2018年 公司名稱. All rights reserved. //
import註釋:若是有一個以上的import語句,對這些語句進行分組,每一個分組的註釋是可選的。xcode
// Framework #import <UIKit/UIKit.h> // Model #import "WTUser.h" // View #import "WTView.h"
方法註釋:Xcode8以後快捷鍵自動生成(option + command + /)。網絡
/** <#Description#> @param application <#application description#> @param launchOptions <#launchOptions description#> @return <#return value description#> */ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
代碼塊註釋:單行的用 「// + 空格」 開頭, 多行用「/* */」。架構
實現文件:必須用」#pragma mark -「將方法分組。分組先後優先級:Lifecycle方法 > Public方法 > UI方法 > Data方法 > Event方法 > Private方法(邏輯處理等) > Delegate方法 > 部分Override方法 > Setter方法 > Getter方法。app
#pragma mark - Lifecycle - (instancetype)init {} - (void)viewDidLoad {} - (void)viewWillAppear:(BOOL)animated {} - (void)viewDidAppear:(BOOL)animated {} - (void)viewWillDisappear:(BOOL)animated {} - (void)viewDidDisappear:(BOOL)animated {} - (void)didReceiveMemoryWarning {} - (void)dealloc {} #pragma mark - Public - (void)refreshData {} #pragma mark - UI - (void)initSubViews {} #pragma mark - Data - (void)initData {} - (void)constructData {} #pragma mark - Event - (void)clickButton:(UIButton *)button {} #pragma mark - Private - (CGFloat)calculateHeight {} #pragma mark - UIScrollViewDelegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView {} #pragma mark - Override - (BOOL)needNavigationBar {} #pragma mark - Setter - (void)setWindow:(UIWindow *)window {} #pragma mark - Getter - (UIWindow *)window {}
變量:優先使用屬性聲明而非變量聲明,注意屬性修飾符、變量類型、變量之間的間隔。ide
@property (strong, nonatomic) UIWindow *window;
間距要求以下:svn
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (door.isClosed) { // Do something } else { // Do something } return YES; }
長度要求以下:函數
名稱一般不縮寫,即便名稱很長也要拼寫徹底(禁止拼音),然而可以使用少數很是常見的縮寫,部分舉例以下:工具
經常使用縮寫詞 | 含義 | 經常使用縮寫詞 | 含義 |
---|---|---|---|
app | application | max | maximum |
alt | alternate | min | minimum |
calc | calculate | msg | message |
alloc | allocte | rect | rectangle |
dealloc | deallocte | msg | message |
init | initialize | temp | temporary |
int | integer | func | function |
屬性本質上是存取方法setter/getter,可進行重寫(注意內存管理)。
@property (strong, nonatomic) UIWindow *window; - (void)setWindow:(UIWindow *)window; - (UIWindow *)window;
能夠適當的對setter/getter進行別名設置。
@property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;
參數前面的單詞要可以描述該參數,而且參數名最好能用描述該參數的單詞命名。
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
方法中多個參數可使用適當的介詞進行鏈接。
// 後續多個參數使用with - (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2; // 添加適當介詞可以使方法的含義更明確 - (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate *)limit; // 第一個參數用了with,後面的參數不使用with - (instancetype)initWithImage:(nullable UIImage *)image highlightedImage:(nullable UIImage *)highlightedImage;
只有在方法返回多個值的時候使用get單詞進行明確。
- (void)getLineDash:(nullable CGFloat *)pattern count:(nullable NSInteger *)count phase:(nullable CGFloat *)phase;
方法返回某個對象實例。
+ (instancetype)buttonWithType:(UIButtonType)buttonType;// 類方法建立對象 + (UIApplication *)sharedApplication;// 單例命名
委託或代理方法命名第一個參數最好能相關某個對象。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
自定義方法和系統方法重名,建議在方法開頭加前綴」xx_methodName「。
宏定義也可根據做用範圍加上適當前綴,避免命名衝突。
枚舉定義時需指定None狀態,而且其rawValue通常爲起始值0。
// NS_ENUM typedef NS_ENUM(NSInteger, UIStatusBarAnimation) { UIStatusBarAnimationNone = 0, UIStatusBarAnimationFade = 1, UIStatusBarAnimationSlide = 2, } typedef NS_OPTIONS(NSUInteger, UIRemoteNotificationType) { UIRemoteNotificationTypeNone = 0, UIRemoteNotificationTypeBadge = 1 << 0, UIRemoteNotificationTypeSound = 1 << 1, UIRemoteNotificationTypeAlert = 1 << 2, UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3, }
通知的命名通常都是跨文件使用的,需添加項目前綴。
// [相關聯類名或者功能模塊名] + [will/Did](可選) + [描述] + Notification UIApplicationDidEnterBackgroundNotification UIApplicationWillEnterForegroundNotification
圖片文件命名格式:[項目前綴] + [業務] + [類型] + [狀態]。
// 常見類型:logol,icon,img // 常見狀態:normal,selected,highlight UIImage *image = [UIImage imageNamed:@"wt_mine_setting_normal"];
若是須要忽略警告,建議優先代碼push或者pop處理。
#pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-retain-cycles" // 形成警告的代碼 #pragma clang diagnostic pop
若是警告數量過大,檢查警告類型以及必要性,可xcode配置忽略此類型警告。步驟:選中工程 -> TARGETS -> Build Settings -> Other Warning Flags。
忽略單個和全局配置稍有差異,以下舉例: push/pop Other Warning Flags -Wformat —-> -Wno-format -Wunused-variable —-> -Wno-unused-variable -Wundeclared-selector —-> -Wno-undeclared-selector -Wint-conversion —-> -Wno-int-conversion
git文件管理配置:.gitignore_global爲全局配置,而倉庫目錄下的.gitignore文件僅註明本倉庫被git忽略的文件,常見語法以下:
提交信息規範: