haoht / RYLazyProperty

好玩的Runtime --- 一行代码解决懒加载

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

好玩的Runtime: 一行代码解决懒加载

解决问题:

  1. 更方便的懒加载
- (Animal *)animal {
    if (_animal) {
        _animal = [Animal new];
    }
    return _layerView;
}
  1. 部分数据的容错
self.nameLabel.text = [NSString stringWithFormat:@"动物名称:%@", self.animal.name];

使用方法:

[Animal ry_setLazyPropertyArr:@[@"name",@"foots",@"attribute",]];
[ViewController ry_setLazyPropertyArr:@[@"animal"]];

该方法仅支持 Objective-C 对象

在调用通过 ry_setLazyPropertyArr 设置的实例变量的时候, 如果该实例变量为 nil, 如果这个实例变量被重写了set方法会调用重写后的 set 方法, 否则会通过[[Class alloc] init]生成一个对象(不会影响KVO).

About

好玩的Runtime --- 一行代码解决懒加载


Languages

Language:Objective-C 100.0%