Objective-C編碼規(guī)范總結(jié)

命名

  • 應(yīng)使用英語命名,而不是中文拼音
    推薦
NSString *password = self._passField.text

不推薦

NSString *mima = self._passField.text
  • 不要使用含義不明的簡寫,盡量寫全單詞,如有必要簡寫,含義需清晰易懂
    推薦
backgroundInitial//或者backgroundInit```
**不推薦**
  • 駝峰命名,常量首字母大寫,屬性和實(shí)例變量首字母小寫,類名和協(xié)議名首字母大寫
    @interface VistorUpgradeViewController : DefaultThemeViewController
    @end//類
    int const CityCounts = 100;//常量
    NSString *currentCity;//變量
    @property (strong, nonatomic) NSString *descriptiveVariableName;//屬性
  • 屬性不要加下劃線前綴
    推薦
@property(strong, nonatomic) UITextField *pwdConfirmField;

不推薦

@property(strong, nonatomic) UITextField *_pwdConfirmField;
  • 添加類別時(shí),方法名可添加前綴,防止重名
    推薦
@interface NSDate (ZOCTimeExtensions)
-(NSString *)zoc_timeAgoShort;
@end

不推薦

@interface NSDate (ZOCTimeExtensions)
-(NSString *)timeAgoShort;//如果NSDate類本身已有該方法,則會(huì)導(dǎo)致方法被該類別覆蓋
@end

代碼組織

  • 使用#pragma mark -進(jìn)行代碼分段,分段方式可以是:getter/setter -- life cycle -- delegate -- event response -- private method
代碼組織

縮進(jìn)與格式

  • 使用xcode默認(rèn)的偏好設(shè)置,即使用tab或者4個(gè)空格
Xcode縮進(jìn)設(shè)置
  • 大括號(hào)開始在方法名同一行,結(jié)束在單獨(dú)一行
    推薦
#pragma mark - action
-(void)cancelBtnPressed:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

不推薦

#pragma mark - action
-(void)cancelBtnPressed:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
  • 方法參數(shù)較多時(shí),用分號(hào)對(duì)齊分行顯示
    推薦
-(void)checkVerifyCode:(NSString*)phoneNum
                withCode:(NSString*)code
             withSuccess:(void(^)(InterfaceModal*))onSuccess
                withFail:(void(^)(NSString*))onFail;

不推薦

-(void)checkVerifyCode:(NSString*)phoneNum withCode:(NSString*)code withSuccess:(void(^)(InterfaceModal*))onSuccess withFail:(void(^)(NSString*))onFail;
  • 函數(shù)分行時(shí),如果第一個(gè)名稱過短,后續(xù)名稱可以以Tab的長度(4個(gè)空格)為單位進(jìn)行縮進(jìn)
    推薦
-(void)short:(GTMFoo *)theFoo
        longKeyword:(NSRect)theRect
  evenLongerKeyword:(float)theInterval
              error:(NSError **)theError {
};

不推薦

-(void)short:(GTMFoo *)theFoo
toolongKeyword:(NSRect)theRect
  evenLongerKeyword:(float)theInterval
  error:(NSError **)theError {
};

注釋

xcode8集成了以前的VVDocument插件,使用option+command+/可實(shí)現(xiàn)對(duì)方法或?qū)傩缘淖⑨?/p>

注釋

枚舉

使用oc風(fēng)格的枚舉
推薦

typedef NS_ENUM(NSInteger,LoginWay){//oc風(fēng)格
    LoginWayUser,
    LoginWayVistor,
};

不推薦

typedef enum{//c風(fēng)格
    LoginWayUser,
    LoginWayVistor,
}LoginWay;

self.和_訪問實(shí)例變量

當(dāng)init,dealloc方法被執(zhí)行時(shí),類的運(yùn)行時(shí)環(huán)境不是處于正常狀態(tài)的,使用存取方法訪問變量可能會(huì)導(dǎo)致不可預(yù)料的結(jié)果,因此應(yīng)當(dāng)在這兩個(gè)方法內(nèi)直接訪問實(shí)例變量
推薦

- (instancetype)initWithNum:(NSString *)num
{
    self = [super init];
    if (self) {
        _phoneNum = num;
    }
    return self;
}

不推薦

- (instancetype)initWithNum:(NSString *)num
{
    self = [super init];
    if (self) {
        self.phoneNum = num;
    }
    return self;
}

判斷是否為空

推薦

if (self.codeTextField.text.length == 0 || !self.codeTextField.text) {
        [self showMessageBox:@"驗(yàn)證碼不能為空" mode:MBProgressHUDModeText];
        [self hideMessageBox];
    }

不推薦

if (self.codeTextField.text.length == 0 || self.codeTextField.text == nil) {
        [self showMessageBox:@"驗(yàn)證碼不能為空" mode:MBProgressHUDModeText];
        [self hideMessageBox];
    }

條件語句

  • 條件語句主體為了防止出錯(cuò)應(yīng)該使用大括號(hào)包圍,即使條件語句主體能夠不用大括號(hào)編寫(如,只用一行代碼)
    推薦
if (!error) {
  return success;
}

不推薦

if (!error)  return success;
  • 當(dāng)if判斷有多層嵌套,應(yīng)該盡量使用黃金路徑,即不符合條件的分支先返回,這樣可以避免分支嵌套
    推薦
-(void)someMethod {
    if (!condition1) {
        return;
    }
    //Do something1
    if (!condition2) {
        return;
    }
    // Do something 2
}

不推薦

-(void)someMethod {
    if (condition1) {
        //Do something1
        if (condition2) {
            // Do something 2
        }
    }
}

宏定義

盡量少使用宏來定義常量,因?yàn)槭褂么罅亢?,容易造成編譯時(shí)間久,每次都需要重新替換??捎胏onst來代替。
推薦

#ifndef GlobalConstant_h
#define GlobalConstant_h

const int TABLEHEIGHT = 10;

#endif /* GlobalConstant_h */

不推薦

#ifndef GlobalConstant_h
#define GlobalConstant_h

#define TABLEHEIGHT 10

#endif /* GlobalConstant_h */
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容