代碼規范

頭文件#import的順序

寫法模板
import <系統庫>
import <第三方庫>
import “其他類”
盡量按照先系統類 第三方類 自己寫的類順序導入 中間不能有空格

建議的寫法

import <UIKit/UIKit.h>
import "ViewController.h"
import "IOSMD5.h"

不建議的寫法

import "ViewController.h"
import "IOSMD5.h"
import <UIKit/UIKit.h>

代碼組織

在函數分組和protocol/delegate實現中使用#pragma mark -來分類方法,要遵循以下一般結構:

#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}

空格

縮進使用4個空格,確保在Xcode偏好設置來設置。
方法大括號和其他大括號(if/else/switch/while 等.)總是在同一行語句打開但在新行中關閉。

建議的寫法

if (user.isHappy) { 
    //Do something
} else { 
    //Do something else
}

不建議的寫法

if (user.isHappy)
{ 
    //Do something
}
else 
{ 
    //Do something else
}

方法參數之間換行

應該避免以冒號對齊的方式來調用方法。因為有時方法簽名可能有3個以上的冒號和冒號對齊會使代碼更加易讀。請不要這樣做,盡管冒號對齊的方法包含代碼塊,因為Xcode的對齊方式令它難以辨認。

建議的寫法

// blocks are easily readable
[UIView animateWithDuration:1.0 animations:^{ 
    // something
} completion:^(BOOL finished) { 
    // something
}];

不建議的寫法

// colon-aligning makes the block indentation hard to read
[UIView animateWithDuration:1.0 
                              animations:^{ 
                                  // something 
                              } completion:^(BOOL finished) { 
                                 // something 
                             }];

@Class的寫法

建議的寫法

@class SecondViewController, ChangeViewController;

不建議的寫法

@class SecondViewController;
@class ChangeViewController;

@Interface的寫法

寫法模板
@interface 類名 : 父類 <協議1, 2="">
@interface和類名中間一個空格
類名后緊跟:之后空格加上父類協議之間用,空格分割

建議的寫法

@interface AppDelegate : UIResponder <UIApplicationDelegate>

不建議的寫法

@interface AppDelegate:UIResponder<UIApplicationDelegate>

@protocol的寫法

寫法的模板
@protocol 協議的名稱 <協議1, 2="">
@potocol和協議的名稱有空格 協議的名稱和其他協議有空格 其他協議之間有空格

建議的寫法

@protocol AHShoppingCartPreparePayProtocal <NSObject>

不建議的寫法

@protocol AHShoppingCartPreparePayProtocal<NSObject>

@property的寫法

@property (關鍵詞, 關鍵詞) 類 *變量名稱;
關鍵詞用,空格分割 類前后空格

建議的寫法

@property (nonatomic, strong) UIButton *userSettingBtn;

不建議的寫法

@property(nonatomic, strong) UIButton * userSettingBtn;

h頭文件方法寫法

寫法模板
@interface
方法的參數在一排顯示
方法之間保留一行
第一個方法和@interface保留空行
最后一個方法和@end保留空行

建議的寫法

@interface AHProductShowViewController : UIViewController

- (void)setWidgetInfo:(AHWidgetInfo *)widgetInfo;

@end

不建議的寫法

@interface AHProductShowViewController : UIViewController
- (void)setWidgetInfo:(AHWidgetInfo *)widgetInfo;
@end

點符號語法

點語法是一種很方便封裝訪問方法調用的方式。當你使用點語法時,通過使用getter或setter方法,屬性仍然被訪問或修改。
點語法應該總是被用來訪問和修改屬性,因為它使代碼更加簡潔。[] 符號更偏向于用在其他例子。

建議的寫法

NSInteger arrayCount = self.array.count;
view.backgroundColor = [UIColor orangeColor];
[UIApplication sharedApplication].delegate;

不建議的寫法

NSInteger arrayCount = [self.array count];
[view setBackgroundColor:[UIColor orangeColor]];
[[UIApplication sharedApplication] delegate];

注釋一定要寫

自己管理的類一定注釋屬性用途 方法的用途 參數的說明
屬性如果設置默認值 一定注明默認值是什么
如果方法內部存在邏輯判斷 方法跳轉 一定注釋判斷用法 方法跳轉用法
除了初始化操作
其他聲明變量 賦值 判斷 應該注明注釋用途

注釋的寫法

Class類注釋

/**
 控制的 cell
 */
@interface CRMControlTableViewCell : UITableViewCell

property屬性的注釋

/** 
  已經選擇的字段 
  */
@property (nonatomic, strong) NSMutableArray *selecteds;

方法的注釋

如果有返回值 請加上return

/**
 @method isCustomerContantImportByName:phone:
 @abstract  根據姓名和電話號查詢顧客
 @discussion 根據姓名和電話號查詢顧客
 @param name customer's name phone customer's phone
 @result 返回一組一個字符串(男/女/nil)對象
 */
+ (NSString *)isCustomerContantImportByName:(NSString *)name phone:(NSString *)phoneNumber;

Block注釋

/**
 @method syncDeviceUpdateTimeSuccess:failure:
 @abstract 客戶端修改設備更新時間的接口
 @discussion 客戶端修改設備更新時間的接口
 @param onSuccess 成功回調
 @param onFailure 失敗回調
 */
- (void)syncDeviceUpdateTimeSuccess:(void (^)())onSuccess failure:(void (^)(NSError *))onFailure;

NSUM 的注釋

/*!
  當前輸入框的狀態
  - ATFVEditStateNormal  : 默認 還沒有輸入任何的文字
  - ATFVEditStateEditing  :  正在進行輸入
  - ATFVEditStateEdited   :  輸入完畢
  - ATFVEditStateNoEdit  :  不允許編輯
  */
typedef NS_ENUM(NSUInteger , ATFVEditState) {
    ATFVEditStateNormal,
    ATFVEditStateEditing,
    ATFVEditStateEdited,
    ATFVEditStateNoEdit
};

計算符號兩邊要有空格

比如 + - * / =等運算符左右有空格

建議的寫法

x = 1 + 2;

不建議的寫法

x=1+2;

控件命名的規范

對于命名一定不要簡寫 那篇很長的單詞 但是一些單詞就是簡寫的除外 比如WTO RMB
UILabel結尾加上Label;
UIImageView結尾記上ImageView
等等讓其他的編程人員看名字就知道變量的用法 和屬于什么控件

建議的寫法

@property (nonatomic, strong) UILabel *nameLabel;

不建議的寫法

@property (nonatomic, strong) UILabel *name;

對于#define宏命名

單詞全部的大寫 單詞之間用_分割

建議的寫法

#define WEB_ROOT_IP @"qa.amway.com.cn"

不建議的寫法

#define kAHPaymentCenterRelease @"NO"

對象調用方法要留空格

建議的寫法

[[NSNotificationCenter defaultCenter] removeObserver:self];

不建議的寫法

[[NSNotificationCenter defaultCenter]removeObserver:self];

對于局部的變量盡量的初始化

局部的變量要初始化 屬性有默認的值

建議的寫法

int index = 0;

不建議的寫法

int index;

變量名的規范

一定要使用駝峰的命名

建議的寫法

CRMNetService *netService = [CRMNetService sharedInstance];

不建議的寫法

CRMNetService *netservice = [CRMNetService sharedInstance];

使用NSUserDefaults要先創建

因為我們用到NSUserDefaults無非是保存和讀取 事先的創建一個對象 可以精簡代碼
當執行方法很多 用變量替換

建議的寫法

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *lastSyncTime = [userDefaults valueForKey:[self lastSyncTimeKey]];

不建議的寫法

[[NSUserDefaults standardUserDefaults] valueForKey:[self lastSyncTimeKey]];

條件語句

條件語句主體為了防止出錯應該使用大括號包圍,即使條件語句主體能夠不用大括號編寫(如,只用一行代碼)。這些錯誤包括添加第二行代碼和期望它成為if語句;還有,even more dangerous defect可能發生在if語句里面一行代碼被注釋了,然后下一行代碼不知不覺地成為if語句的一部分。除此之外,這種風格與其他條件語句的風格保持一致,所以更加容易閱讀。

建議的寫法

if (!error) {
    return success;
}

不建議的寫法

if (!error) return success;

黃金路徑

當使用條件語句編碼時,左手邊的代碼應該是"golden" 或 "happy"路徑。也就是不要嵌套if語句,多個返回語句也是OK。

建議的寫法

- (void)someMethod { 
    if (![someOther boolValue]) { 
        return; 
    } 
    //Do something important
}

不建議的寫法

- (void)someMethod { 
    if ([someOther boolValue]) { 
        //Do something important 
    }
}

錯誤處理

當方法通過引用來返回一個錯誤參數,判斷返回值而不是錯誤變量。

建議的寫法

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

不建議的寫法

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

類名加上前綴避免沖突

因為團隊的合作 可能會出現大家想到一樣的名字或者添加第三方庫引入和第三方庫名字一樣
盡量加上前綴。
如果只針對工程就使用工程的縮寫
比如自己個人的第三方庫就加上自己名字或者昵稱的縮寫

建議的寫法

@interface CRMAddEditGroupViewController : UIViewController

不建議的寫法

@interface AddEditGroupViewController : UIViewController

盡可能使用不可變的對象

對于OC存在很多可變的對象 比如NSMutableString NSMutableArray NSMutableDictionary等等
對于一些不允許改變的直接使用不可變對象
可以節省對象開支 還可以防止別人修改數據造成bug

建議的寫法

NSArray *sexList = @[@"男",@"女"];

不建議的寫法

NSMutableArray *sexList = [NSMutableArray arrayWithArray:@[@"男",@"女"]];

對于一些自己不確定的可以使用try catch

對于不知道后臺返回什么類型的 可以使用try catch

建議的寫法

int index = 0;
@try {
    NSArray *array = obj[@"list"];
    index = [array.firstObject intValue];
}
@catch { }

不建議的寫法

int index = 0;
NSArray *array = obj[@"list"];
if (array.count > 0) {
    index = [array.firstObject intValue];
}

使用dispatch_once來創建單例

建議的寫法

+ (instancetype)sharedInstance { 
    static id sharedInstance = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
        sharedInstance = [[self alloc] init]; 
    });
    return sharedInstance;
}

便利的寫法

如果只需要便利數組和字典的寫法用for in

建議的寫法

for (NSString *name in names) {

}

不建議的寫法

for (int i = 0; i < names.lenght; i ++) {

}

字典的元素垂直寫

建議的寫法

NSDictionary *dictionary = @{
                                                   @"a" : @"",
                                                   @"b" : @"",
                                                   @"c" : @""
                                                 };

不建議的寫法

NSDictionary *dictionary = @{@"a" : @"", @"b" : @"", @"c" : @"" }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • GB代碼規范 頭文件#import的順序(商量) 寫法模板#import <系統庫>#import <第三方庫>#...
    君賞閱讀 1,029評論 0 1
  • iOS編程規范0規范 0.1前言 為??高產品代碼質量,指導廣大軟件開發人員編寫出簡潔、可維護、可靠、可 測試、高效...
    iOS行者閱讀 4,502評論 21 35
  • 推薦文章:禪與 Objective-C 編程藝 前言 為??高產品代碼質量,指導廣大軟件開發人員編寫出簡潔、可維護、...
    WolfTin閱讀 2,827評論 0 1
  • 復古風可是把以往的流行元素重新吹了一遍,其實時尚都是這樣周而復始的。近段時間那個亮亮的絲絨又火起來了。 說實話,在...
    舉個LEE子閱讀 442評論 0 0
  • 本地私有庫不能滿足團隊開發的需要,存在一下問題: 需要主動指明路徑,然后庫的本地位置就不能動了,否則Podfile...
    Lovell_閱讀 402評論 0 0