變量
- 變量名稱遵循駝峰命名法則
- 變量名稱采用英文命名且命名所見即所得,明確變量的意義。不得存在拼音或含糊不清的英文表達
- 變量的指針位置遵循以下寫法
NSString *userName; //指針需要靠近變量名而不是變量類型
- 成員變量需要加上以
m_
為前綴,如:m_userName
函數(shù)
- 函數(shù)需按照以下寫法:
- (void)exampleFuntion:(NSString *)param {
//函數(shù)開始符- 與(void)之間要有一個空格
//{}首個符號不能換行且需要與參數(shù)留有一個空格
}
- 如果函數(shù)參數(shù)有多個的時候骄崩,將每個參數(shù)單獨拆成一行,每個參數(shù)的冒號對齊
- (void)doSomethingWith:(NSString *)theFoo
rect:(CGRect)rect
interval:(CGFloat)interval {
...
}
- 方法調(diào)用的時候應(yīng)盡量保持與方法聲明的格式一致。當(dāng)格式的風(fēng)格有多種選擇時吏夯,新的代碼要與已有代碼保持一致
調(diào)用時所有參數(shù)應(yīng)該在同一行:
[myObject doFooWith:arg1 name:arg2 error:arg3];
或者每行一個參數(shù),以冒號對齊:
[myObject doFooWith:arg1
name:arg2
error:arg3];
協(xié)議名
- 類型標識符和尖括號內(nèi)的協(xié)議名之間即横,不能有任何空格
這條規(guī)則適用于類聲明噪生、實例變量以及方法聲明。例如:
@interface MyProtocoledClass : NSObject<NSWindowDelegate> {
@private id<MyFancyDelegate> delegate_;
}
- (void)setDelegate:(id<MyFancyDelegate>)aDelegate;
@end
注釋
- 注釋采用
VVDocument
工具進行注釋
nil的檢查
*要注意檢查判斷邊界
枚舉
采用如下方式寫枚舉
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // regular table view
UITableViewStyleGrouped // preferences style table view
};
宏定義
- 宏定義都寫在框架統(tǒng)一的文件里面
pragma Mark
pragma mark -
是一個在類內(nèi)部組織代碼并且?guī)椭惴纸M方法實現(xiàn)的好辦法东囚。 我們建議使用 #pragma mark -
來分離:
- 不同功能組的方法
- protocols 的實現(xiàn)
- 對父類方法的重寫
- (void)dealloc { /* ... */ }
- (instancetype)init { /* ... */ }
#pragma mark - View Lifecycle (View 的生命周期)
- (void)viewDidLoad { /* ... */ }
- (void)viewWillAppear:(BOOL)animated { /* ... */ }
- (void)didReceiveMemoryWarning { /* ... */ }
#pragma mark - Custom Accessors (自定義訪問器)
- (void)setCustomProperty:(id)value { /* ... */ }
- (id)customProperty { /* ... */ }
#pragma mark - IBActions
- (IBAction)submitData:(id)sender { /* ... */ }
#pragma mark - Public
- (void)publicMethod { /* ... */ }
#pragma mark - Private
- (void)zoc_privateMethod { /* ... */ }
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { /* ... */ }
#pragma mark - ZOCSuperclass
// ... 重載來自 ZOCSuperclass 的方法
#pragma mark - NSObject
- (NSString *)description { /* ... */ }
self 的循環(huán)引用
當(dāng)使用代碼塊和異步分發(fā)的時候跺嗽,要注意避免引用循環(huán)。 總是使用 weak
來引用對象,避免引用循環(huán)桨嫁。此外植兰,把持有 block 的屬性設(shè)置為 nil (比如self.completionBlock = nil
) 是一個好的實踐。它會打破 block 捕獲的作用域帶來的引用循環(huán)璃吧。
**例子:**
__weak __typeof(self) weakSelf = self;
[self executeBlock:^(NSData *data, NSError *error) {
[weakSelf doSomethingWithData:data];
}];
**不要這樣:**
[self executeBlock:^(NSData *data, NSError *error) {
[self doSomethingWithData:data];
}];
**多個語句的例子:**
__weak __typeof(self)weakSelf = self;
[self executeBlock:^(NSData *data, NSError *error) {
__strong __typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf doSomethingWithData:data];
[strongSelf doSomethingWithData:data];
}
}];
**不要這樣:**
__weak __typeof(self)weakSelf = self;
[self executeBlock:^(NSData *data, NSError *error) {
[weakSelf doSomethingWithData:data];
[weakSelf doSomethingWithData:data];
}];
待續(xù)...