懒加载

懒加载就是在getter方法里,判断成员变量是否为空,如果为空给成员变量赋值。

// 加载plist数据(比较大)
// 懒加载:用到时再去加载,而且也只加载一次
- (NSArray *)shops
{
    if (_shops == nil) {
        NSString *file = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
        self.shops = [NSArray arrayWithContentsOfFile:file];
//        _shops = [NSArray arrayWithContentsOfFile:file];
//        [self setShops:[NSArray arrayWithContentsOfFile:file]];
    }
    return _shops;
}

你可能感兴趣的:(懒加载)