@property (strong,nonatomic) NSMutableArray *helps;
-(NSMutableArray *)helps{
if (_helps == nil) {
//得到文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"json"];
//使用NSData来接受数据
NSData *data = [NSData dataWithContentsOfFile:path];
//得到字典数组,第二个参数枚举值表示加载出来的数组是可变的
NSArray *helpArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
_helps = [NSMutableArray new];
for (NSDictionary *dic in helpArray) {
//将字典数组转换成模型数组
DYHelp *help = [[DYHelp alloc] initWithDic:dic];
//将模型存入到属性中
[_helps addObject:help];
}
}
return _helps;
}
注意点:
- 懒加载中if(_helps) 必须是_helps,不可以写成self.helps 同理retun _helps,使用self.helps会造成死循环
- 调用helps数据时必须使用self.helps才会执行get方法(懒加载),如果是_helps则不会执行