頭文件#import的順序
寫法模板
import <系統(tǒng)庫>
import <第三方庫>
import “其他類”
盡量按照先系統(tǒng)類 第三方類 自己寫的類順序?qū)?中間不能有空格
建議的寫法
import <UIKit/UIKit.h>
import "ViewController.h"
import "IOSMD5.h"
不建議的寫法
import "ViewController.h"
import "IOSMD5.h"
import <UIKit/UIKit.h>
代碼組織
在函數(shù)分組和protocol/delegate實(shí)現(xiàn)中使用#pragma mark -
來分類方法屿衅,要遵循以下一般結(jié)構(gòu):
#pragma mark - Lifecycle
- (instancetype)init {}
- (void)dealloc {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}
- (void)didReceiveMemoryWarning {}
空格
縮進(jìn)使用4個空格蚁署,確保在Xcode偏好設(shè)置來設(shè)置伴鳖。
方法大括號和其他大括號(if/else/switch/while 等.)總是在同一行語句打開但在新行中關(guān)閉。
建議的寫法
if (user.isHappy) {
//Do something
} else {
//Do something else
}
不建議的寫法
if (user.isHappy)
{
//Do something
}
else
{
//Do something else
}
方法參數(shù)之間換行
應(yīng)該避免以冒號對齊的方式來調(diào)用方法。因?yàn)橛袝r方法簽名可能有3個以上的冒號和冒號對齊會使代碼更加易讀。請不要這樣做,盡管冒號對齊的方法包含代碼塊宣肚,因?yàn)閄code的對齊方式令它難以辨認(rèn)。
建議的寫法
// 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 類名 : 父類 <協(xié)議1, 2="">
@interface和類名中間一個空格
類名后緊跟:之后空格加上父類協(xié)議之間用,空格分割
建議的寫法
@interface AppDelegate : UIResponder <UIApplicationDelegate>
不建議的寫法
@interface AppDelegate:UIResponder<UIApplicationDelegate>
@protocol的寫法
寫法的模板
@protocol 協(xié)議的名稱 <協(xié)議1, 2="">
@potocol和協(xié)議的名稱有空格 協(xié)議的名稱和其他協(xié)議有空格 其他協(xié)議之間有空格
建議的寫法
@protocol AHShoppingCartPreparePayProtocal <NSObject>
不建議的寫法
@protocol AHShoppingCartPreparePayProtocal<NSObject>
@property的寫法
@property (關(guān)鍵詞, 關(guān)鍵詞) 類 *變量名稱;
關(guān)鍵詞用,空格分割 類前后空格
建議的寫法
@property (nonatomic, strong) UIButton *userSettingBtn;
不建議的寫法
@property(nonatomic, strong) UIButton * userSettingBtn;
h頭文件方法寫法
寫法模板
@interface
方法的參數(shù)在一排顯示
方法之間保留一行
第一個方法和@interface保留空行
最后一個方法和@end保留空行
建議的寫法
@interface AHProductShowViewController : UIViewController
- (void)setWidgetInfo:(AHWidgetInfo *)widgetInfo;
@end
不建議的寫法
@interface AHProductShowViewController : UIViewController
- (void)setWidgetInfo:(AHWidgetInfo *)widgetInfo;
@end
點(diǎn)符號語法
點(diǎn)語法是一種很方便封裝訪問方法調(diào)用的方式悠栓。當(dāng)你使用點(diǎn)語法時,通過使用getter或setter方法按价,屬性仍然被訪問或修改惭适。
點(diǎn)語法應(yīng)該總是被用來訪問和修改屬性,因?yàn)樗勾a更加簡潔楼镐。[] 符號更偏向于用在其他例子癞志。
建議的寫法
NSInteger arrayCount = self.array.count;
view.backgroundColor = [UIColor orangeColor];
[UIApplication sharedApplication].delegate;
不建議的寫法
NSInteger arrayCount = [self.array count];
[view setBackgroundColor:[UIColor orangeColor]];
[[UIApplication sharedApplication] delegate];
注釋一定要寫
自己管理的類一定注釋屬性用途 方法的用途 參數(shù)的說明
屬性如果設(shè)置默認(rèn)值 一定注明默認(rèn)值是什么
如果方法內(nèi)部存在邏輯判斷 方法跳轉(zhuǎn) 一定注釋判斷用法 方法跳轉(zhuǎn)用法
除了初始化操作
其他聲明變量 賦值 判斷 應(yīng)該注明注釋用途
注釋的寫法
Class類注釋
/**
控制的 cell
*/
@interface CRMControlTableViewCell : UITableViewCell
property屬性的注釋
/**
已經(jīng)選擇的字段
*/
@property (nonatomic, strong) NSMutableArray *selecteds;
方法的注釋
如果有返回值 請加上return
/**
@method isCustomerContantImportByName:phone:
@abstract 根據(jù)姓名和電話號查詢顧客
@discussion 根據(jù)姓名和電話號查詢顧客
@param name customer's name phone customer's phone
@result 返回一組一個字符串(男/女/nil)對象
*/
+ (NSString *)isCustomerContantImportByName:(NSString *)name phone:(NSString *)phoneNumber;
Block注釋
/**
@method syncDeviceUpdateTimeSuccess:failure:
@abstract 客戶端修改設(shè)備更新時間的接口
@discussion 客戶端修改設(shè)備更新時間的接口
@param onSuccess 成功回調(diào)
@param onFailure 失敗回調(diào)
*/
- (void)syncDeviceUpdateTimeSuccess:(void (^)())onSuccess failure:(void (^)(NSError *))onFailure;
NSUM 的注釋
/*!
當(dāng)前輸入框的狀態(tài)
- ATFVEditStateNormal : 默認(rèn) 還沒有輸入任何的文字
- ATFVEditStateEditing : 正在進(jìn)行輸入
- ATFVEditStateEdited : 輸入完畢
- ATFVEditStateNoEdit : 不允許編輯
*/
typedef NS_ENUM(NSUInteger , ATFVEditState) {
ATFVEditStateNormal,
ATFVEditStateEditing,
ATFVEditStateEdited,
ATFVEditStateNoEdit
};
計(jì)算符號兩邊要有空格
比如 + - * / =等運(yùn)算符左右有空格
建議的寫法
x = 1 + 2;
不建議的寫法
x=1+2;
控件命名的規(guī)范
對于命名一定不要簡寫 那篇很長的單詞 但是一些單詞就是簡寫的除外 比如WTO RMB
UILabel結(jié)尾加上Label;
UIImageView結(jié)尾記上ImageView
等等讓其他的編程人員看名字就知道變量的用法 和屬于什么控件
建議的寫法
@property (nonatomic, strong) UILabel *nameLabel;
不建議的寫法
@property (nonatomic, strong) UILabel *name;
對于#define宏命名
單詞全部的大寫 單詞之間用_分割
建議的寫法
#define WEB_ROOT_IP @"qa.amway.com.cn"
不建議的寫法
#define kAHPaymentCenterRelease @"NO"
對象調(diào)用方法要留空格
建議的寫法
[[NSNotificationCenter defaultCenter] removeObserver:self];
不建議的寫法
[[NSNotificationCenter defaultCenter]removeObserver:self];
對于局部的變量盡量的初始化
局部的變量要初始化 屬性有默認(rèn)的值
建議的寫法
int index = 0;
不建議的寫法
int index;
變量名的規(guī)范
一定要使用駝峰的命名
建議的寫法
CRMNetService *netService = [CRMNetService sharedInstance];
不建議的寫法
CRMNetService *netservice = [CRMNetService sharedInstance];
使用NSUserDefaults要先創(chuàng)建
因?yàn)槲覀冇玫絅SUserDefaults無非是保存和讀取 事先的創(chuàng)建一個對象 可以精簡代碼
當(dāng)執(zhí)行方法很多 用變量替換
建議的寫法
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *lastSyncTime = [userDefaults valueForKey:[self lastSyncTimeKey]];
不建議的寫法
[[NSUserDefaults standardUserDefaults] valueForKey:[self lastSyncTimeKey]];
條件語句
條件語句主體為了防止出錯應(yīng)該使用大括號包圍,即使條件語句主體能夠不用大括號編寫(如框产,只用一行代碼)凄杯。這些錯誤包括添加第二行代碼和期望它成為if語句;還有秉宿,even more dangerous defect可能發(fā)生在if語句里面一行代碼被注釋了戒突,然后下一行代碼不知不覺地成為if語句的一部分。除此之外描睦,這種風(fēng)格與其他條件語句的風(fēng)格保持一致膊存,所以更加容易閱讀。
建議的寫法
if (!error) {
return success;
}
不建議的寫法
if (!error) return success;
黃金路徑
當(dāng)使用條件語句編碼時忱叭,左手邊的代碼應(yīng)該是"golden" 或 "happy"路徑隔崎。也就是不要嵌套if
語句,多個返回語句也是OK韵丑。
建議的寫法
- (void)someMethod {
if (![someOther boolValue]) {
return;
}
//Do something important
}
不建議的寫法
- (void)someMethod {
if ([someOther boolValue]) {
//Do something important
}
}
錯誤處理
當(dāng)方法通過引用來返回一個錯誤參數(shù)爵卒,判斷返回值而不是錯誤變量。
建議的寫法
NSError *error;
if (![self trySomethingWithError:&error]) {
// Handle Error
}
不建議的寫法
NSError *error;
[self trySomethingWithError:&error];
if (error) {
// Handle Error
}
類名加上前綴避免沖突
因?yàn)閳F(tuán)隊(duì)的合作 可能會出現(xiàn)大家想到一樣的名字或者添加第三方庫引入和第三方庫名字一樣
盡量加上前綴撵彻。
如果只針對工程就使用工程的縮寫
比如自己個人的第三方庫就加上自己名字或者昵稱的縮寫
建議的寫法
@interface CRMAddEditGroupViewController : UIViewController
不建議的寫法
@interface AddEditGroupViewController : UIViewController
盡可能使用不可變的對象
對于OC存在很多可變的對象 比如NSMutableString NSMutableArray NSMutableDictionary等等
對于一些不允許改變的直接使用不可變對象
可以節(jié)省對象開支 還可以防止別人修改數(shù)據(jù)造成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來創(chuàng)建單例
建議的寫法
+ (instancetype)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
便利的寫法
如果只需要便利數(shù)組和字典的寫法用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" : @"" }