代码规范

284 阅读3分钟

代码组织

  • ViewController代码组织
  • Custom View(包括cell)代码组织

1.ViewController代码组织

@property (nonatomic, strong) NSObject *obj;
...

#pragma mark - life cycle
- (instancetype)init {}
- (void)viewDidLoad {}
- (void)viewWillAppear:(BOOL)animated {}

#pragma mark - events response
按钮事件
#pragma mark - custom delegate
自定义代理方法
#pragma mark - system delegate
系统代理方法
#pragma mark - private method
如果需要有私有方法 写在这
#pragma mark - getter & setter
属性的getter和setter

2.Custom View(包括cell)代码组织

#pragma mark - life cycle
- (instancetype)init {}

#pragma mark - events

#pragma mark - layout
// frame布局
- (void)layoutSubviews {
	  // 开头调用super方法
    [super layoutSubviews];
    
}

// 约束布局
+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

- (void)updateConstraints {
    // 结尾调用super方法
    [super updateConstraints];
}

#pragma mark - getter

所有的属性初始化都使用getter(懒加载)

代码观感是这样的

@interface CustomView ()

@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UILabel *label3;

@end
@implementation CustomView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.label1];
        [self addSubview:self.label2];
        [self addSubview:self.label3];
    }
    return self;
}

- (void)configModel:(XXXModel *)model {
    self.label1.text = model.name;
    ...
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.label1.frame = CGRectMake(0, 0, 100, 40);
    ...
}

- (UILabel *)label1 {
    if (!_label1) {
        _label1 = [[UILabel alloc] init];
        ...
    }
    return _label1;
}
- (UILabel *)label2 {
    if (!_label2) {
        _label2 = [[UILabel alloc] init];
        ...
    }
    return _label2;
}
- (UILabel *)label3 {
    if (!_label3) {
        _label3 = [[UILabel alloc] init];
    }
    return _label3;
}

@end

这样写的好处是属性的基本配置代码都在getter中,即便属性很多,在进行业务调试的时候只需要关心 - (void)configModel:(XXXModel *)model 中的逻辑,提高了代码的易读性、可维护性。

声明属性的一些方便代码块:

@property (nonatomic, strong) <#Class#> *<#object#>; 
@property (nonatomic, copy) <#Class#> *<#object#>;
@property (nonatomic, assign) <#Class#> *<#object#>;

- (<#Class#> *)<#instanceName#> {
    if (!_<#instanceName#>) {
        <#statements#>
    }
    return _<#instanceName#>;
}

代码规范

1.写方法的时候保持系统书写风格

在方法签名中,应该在方法类型(-/+ 符号)之后有一个空格。在方法各个段之间应该也有一个空格(符合Apple的风格)。

- (void)methodName {
    
}

不要像下面这样写

- (void) methodName {
    
}
-(void)methodName{
    
}
-(void)methodName
{
    
}
...

2.条件判断语句


if (<#condition#>) {
	<#statements#>
}

if (<#condition#>) {
	<#statements#>
} else {
	<#statements#>
}

条件判断语句xcode内置的code-snippet里都有,直接就能敲出来

3.类名类文件首字母大写

4.成员变量的声明应该加下划线前缀

× NSObject *obj;

NSObject *_obj;

5.属性成员变量的声明位置

不需要外部访问的属性、成员变量都不要写在.h中

6.布尔值

应该

if (someObject) {}
if (![anotherObject boolValue]) {}

不应该

if (someObject == nil) {}
if ([anotherObject boolValue] == NO) {}
if (isAwesome == YES) {} // Never do this.
if (isAwesome == true) {} // Never do this.

6.属性声明

property关键字、属性修饰关键字、类名、属性名间均以一个空格隔开。

@property (nonatomic, strong) NSObject *obj;

× @property(nonatomic,strong)NSObject *obj

7.hardcode、常量字符串

尽量避免hardcode,类似这样:

    if (xxcode == 0) {
        //
    } else if (xxcode == 1) {
        //
    }

上面写法的缺点:表意不明,单单是看代码根本不知道每一个code代表什么,时间一长写这段代码的人兴许都记不起来这个code是做什么的;

在这推荐使用枚举(ENUM)的方式来消除这一部分hardcode

typedef NS_ENUM(NSUInteger, <#MyEnum#>) {
    <#MyEnumValueA#>,
    <#MyEnumValueB#>,
    <#MyEnumValueC#>,
};

相应的如果有部分字符串需要在多处使用到,不应该把@""copy到各处,可以使用固定字符串来表示:

//.h
extern NSString * const XXNotificationName;
//.m
NSString * const XXNotificationName = @"XXNotificationName";

外部需要使用的地方导入.h文件就行了,一方面使用字符串有了提示,另一方面也规避了外部修改字符串的风险。

命名方法部分 可以看看这里面=>OC代码规范总结


refs:

Objective-C编码规范[译]

iOS应用架构谈 view层的组织和调用方案

Apple编程规范:

Coding Guidelines for Cocoa