懒加载
懒加载又称为延迟加载,它是指系统不会在初始化时就加载某个对象,而是在第一次调用(使用get方法)时才加载这个对象到内存中。简单来说,就是某个对象被使用的时候再加载。
懒加载的实现方式:重写对象的get方法,并将该对象在初始化时需要实现的代码在get方法中实现。
举例子:
@interface ViewController ()
@property (nonatomic, strong) NSArray *infoArr;//数组
@property (nonatomic, strong) UITableview *tableview;//
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configData];
[self setupTableview];
}
- (void)configData {
_infoArr = @[@{@"title":@"出团日期", @"routeName":@"线路名称一", @"time":@"2015/11/21", @"num":@"20", @"price":@"124.0", @"code":@"DAGSDSASA"},
@{@"title":@"余位", @"routeName":@"线路名称二", @"time":@"2015/11/21", @"num":@"34", @"price":@"234", @"code":@"TAGDFASFAF"},
@{@"title":@"价格", @"routeName":@"线路名称三", @"time":@"2015/11/21", @"num":@"12", @"price":@"634", @"code":@"GHGASDAS"},
@{@"title":@"团代号", @"routeName":@"线路名称四", @"time":@"2015/11/56", @"num":@"54", @"price":@"632", @"code":@"DAADSFAD"}];
}
- (void)setupTableview {
_tableView = [[UITableView alloc] initWithFrame:rc];//(这里写_tableView或者self.tableView 都可以)
_tableView.backgroundColor = YrGetColor(COLOR_TYPE_A);
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_tableView.allowsSelection = YES;
_tableView.scrollsToTop = NO;
_tableView.bounces = NO;
_tableView.scrollEnabled = NO;
_tableView.showsVerticalScrollIndicator = YES;
_tableView.alwaysBounceVertical = YES;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
@end
假设这个_infoArr和_tableView是在某些事件被触发之后才会被调用,这时就没有必要再控制器加载完就去加载这个数组数据了,如果事件不触发就意味着_infoArr永远不会被加载,这样的话在viewDidLoad中把它加载了就显得很多余了并且也是很耗内存的。
懒加载方法:
@interface ViewController ()
@property (nonatomic, strong) NSArray *infoArr;//数组
@property (nonatomic, strong) UITableview *tableview;//
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self infoArr];
[self tableview];
}
//在viewDidLoad方法中调用懒加载:[self infoArr]的方式来调用
- (NSArray *)infoArr {
if (!_infoArr) {
_infoArr = @[@{@"title":@"出团日期", @"routeName":@"线路名称一", @"time":@"2015/11/21", @"num":@"20", @"price":@"124.0", @"code":@"DAGSDSASA"},
@{@"title":@"余位", @"routeName":@"线路名称二", @"time":@"2015/11/21", @"num":@"34", @"price":@"234", @"code":@"TAGDFASFAF"},
@{@"title":@"价格", @"routeName":@"线路名称三", @"time":@"2015/11/21", @"num":@"12", @"price":@"634", @"code":@"GHGASDAS"},
@{@"title":@"团代号", @"routeName":@"线路名称四", @"time":@"2015/11/56", @"num":@"54", @"price":@"632", @"code":@"DAADSFAD"}];
}
return _infoArr;
}
- (UITableview *)tableview {
if (!_tableview) {
_tableview = [[UITableView alloc] initWithFrame:rc];
_tableview.backgroundColor = YrGetColor(COLOR_TYPE_A);
_tableview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
_tableview.allowsSelection = YES;
_tableview.scrollsToTop = NO;
_tableview.bounces = NO;
_tableview.scrollEnabled = NO;
_tableview.showsVerticalScrollIndicator = YES;
_tableview.alwaysBounceVertical = YES;
_tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableview.dataSource = self;
_tableview.delegate = self;
[self.view addSubview:m_tableView];
}
return _tableviewr;
}
使用懒加载的注意点:1,使用懒加载时,必须注意getter方法嵌套(if (!self.tableview)会引起嵌套,return self.tableviewr会引起嵌套;),从而造成死循环,导致程序crash!2,强烈建议使用懒加载时,懒加载内部的实例变量完全采用下划线的方式去判断及创建(if (!_tableview) return _tableviewr)!!!
还有另外一种只要你记住, 就随便你怎么写,并且万无一失、永远也不会造成死循环的方法,那就是懒加载的方法名不要和懒加载内部的实例变量同名,这样Xcode就可以准确识别出你调用的是属性还是懒加载方法,比如:(请对比两种懒加载的写法)
使用懒加载的优点:
-
相对来说,如果代码量不是很多,可读性略强
-
相对来说,防止为nil,减少了后续使用时安全检查的后顾之忧
-
使用适当,可节省内存资源
-
一定程度上,节省了某一个期间内的时间
-
使用得当,优化性能,提高用户体验
-
使用懒加载的缺点:
-
使用太泛滥,导致可读性变差
-
使用不得当,可能会造成死循环,导致crash
-
代码量增多(每增加一个懒加载,代码会平均多出3-4行)