Objective-C编码规范总结

2017-03-06  本文已影响13人  斯乐林

命名

NSString *password = self._passField.text

不推荐

NSString *mima = self._passField.text
backgroundInitial//或者backgroundInit```
**不推荐**
    @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;
@interface NSDate (ZOCTimeExtensions)
-(NSString *)zoc_timeAgoShort;
@end

不推荐

@interface NSDate (ZOCTimeExtensions)
-(NSString *)timeAgoShort;//如果NSDate类本身已有该方法,则会导致方法被该类别覆盖
@end

代码组织

代码组织

缩进与格式

Xcode缩进设置
#pragma mark - action
-(void)cancelBtnPressed:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

不推荐

#pragma mark - action
-(void)cancelBtnPressed:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(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;
-(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+/可实现对方法或属性的注释

注释

枚举

使用oc风格的枚举
推荐

typedef NS_ENUM(NSInteger,LoginWay){//oc风格
    LoginWayUser,
    LoginWayVistor,
};

不推荐

typedef enum{//c风格
    LoginWayUser,
    LoginWayVistor,
}LoginWay;

self.和_访问实例变量

当init,dealloc方法被执行时,类的运行时环境不是处于正常状态的,使用存取方法访问变量可能会导致不可预料的结果,因此应当在这两个方法内直接访问实例变量
推荐

- (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:@"验证码不能为空" mode:MBProgressHUDModeText];
        [self hideMessageBox];
    }

不推荐

if (self.codeTextField.text.length == 0 || self.codeTextField.text == nil) {
        [self showMessageBox:@"验证码不能为空" mode:MBProgressHUDModeText];
        [self hideMessageBox];
    }

条件语句

if (!error) {
  return success;
}

不推荐

if (!error)  return success;
-(void)someMethod {
    if (!condition1) {
        return;
    }
    //Do something1
    if (!condition2) {
        return;
    }
    // Do something 2
}

不推荐

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

宏定义

尽量少使用宏来定义常量,因为使用大量宏,容易造成编译时间久,每次都需要重新替换。可用const来代替。
推荐

#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 */
上一篇 下一篇

猜你喜欢

热点阅读