这该死的代码片断怎么不好使了?搞了N次了
IOS延迟实例化,是在IOS开发中经常用到的一种方式.
假设我们定义有属性:
@property (nonatomic, strong) NSMutableData *webData;
并对其展开,以自动生成setter和getter方法:
@synthesize webData = _webData;
重写其getter方法:
- (NSMutableData *)webData
{
if ( !_webData) {
_webData = [[NSMutableData alloc] init];
}
return _webData;
}
这样的话,webData将会在使用前才进行初始化.