1.例如:你定义了一个可变的数组你需要初始化这个数组这时候就可以使用懒加载的方式
懒加载的好处只有用到的时候才会调用初始化减少了处理时间防止你忘记了对变量进行初始化而导致的崩溃
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *dataSources;
@end
2.下面就是懒加载
@implementation ViewController
-(NSMutableArray *)dataSources{
if (_dataSources == nil) {
_dataSources = [[NSMutableArray alloc]init];
}
return _dataSources;
}
最重要的一点*****3.一定要记住使用了懒加载以后在项目里面只能使用self.dataSources
不然不会调用上面的懒加载方法
网友评论