美文网首页
Objective-C编码规范总结

Objective-C编码规范总结

作者: 斯乐林 | 来源:发表于2017-03-06 11:39 被阅读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

代码组织

  • 使用#pragma mark -进行代码分段,分段方式可以是:getter/setter -- life cycle -- delegate -- event response -- private method
代码组织

缩进与格式

  • 使用xcode默认的偏好设置,即使用tab或者4个空格
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;
  • 函数分行时,如果第一个名称过短,后续名称可以以Tab的长度(4个空格)为单位进行缩进
    推荐
-(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;
  • 当if判断有多层嵌套,应该尽量使用黄金路径,即不符合条件的分支先返回,这样可以避免分支嵌套
    推荐
-(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 */

相关文章

  • iOS客户端开发编码规范

    Objective-C 编码规范,内容来自苹果的文档翻译,自己的编码经验和对其它资料的总结。 一.命名规范 基本原...

  • OC代码规范总结

    Objective-C Coding Guidelines In Chinese Objective-C编码规范,...

  • Objective-C 编码规范

    Objective-C 编码规范,内容来自苹果、谷歌的文档翻译,自己的编码经验和对其它资料的总结。 概要 Obje...

  • Objective-C编码规范

    Objective-C编码规范,内容来自苹果,谷歌的翻译文档,自己的编码经验和对其他资料的总结。 概要 Objec...

  • Objective-C编码规范

    raywenderlich.com Objective-C编码规范 这篇编码风格指南概括了raywenderlic...

  • Objective-C编码规范总结

    命名 应使用英语命名,而不是中文拼音推荐 不推荐 不要使用含义不明的简写,尽量写全单词,如有必要简写,含义需清晰易...

  • iOS代码规范

    禅与 Objective-C 编程艺术 《Google Objective-C 风格指南》 《苹果官方编码规范》

  • # Objective-C-Coding-Guidelines-

    Objective-C-Coding-Guidelines-In-Chinese Objective-C编码规范,...

  • Objective-C编码规范

    Objective-C-Coding-Guidelines-In-Chinese Objective-C编码规范,...

  • Objective-C编码规范

    Objective-C-Coding-Guidelines-In-Chinese Objective-C编码规范,...

网友评论

      本文标题:Objective-C编码规范总结

      本文链接:https://www.haomeiwen.com/subject/dxelgttx.html