iOS如何懒加载json文件中的数据(objc)
一、数据源
我们就模拟从plist文件加载数据
根据json文件中的存储类型来加载(大多数是数组里面存字典),所以我们就演示数组里面存字典的加载,假设数据如下,文件名help.json
[
{
"title" : "如何领奖?",
"html" : "help.html",
"id" : "howtoprize"
},
{
"title" : "如何充值?",
"html" : "help.html",
"id" : "howtorecharge"
},
{
"title" : "如何提现?",
"html" : "help.html",
"id" : "howtowithdraw"
},
{
"title" : "如何购彩?",
"html" : "help.html",
"id" : "howtobuy"
},
{
"title" : "如何连续多期买同一注号码?",
"html" : "help.html",
"id" : "whatisfollowandtimes"
}
]
二、创建模型
首先根据文件中字典的键值对来创建模型,模型的属性要包含要使用的数据的键,模型类名是DYHelp
//------------.h文件
#import <Foundation/Foundation.h>
@interface DYHelp : NSObject
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *html;
@property(nonatomic,copy)NSString *Id;
- (instancetype)initWithDic:(NSDictionary *)dic;
+ (instancetype)helpWithDic:(NSDictionary *)dic;
@end
//------------.m文件
#import "DYHelp.h"
@implementation DYHelp
- (instancetype)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
+(instancetype)helpWithDic:(NSDictionary *)dic{
return [[self alloc] initWithDic:dic];
}
@end
三、懒加载数据
做完上面的准备工作就可以来加载数据了,在需要使用数据的类里面创建一个数组属性,来存储将要加载的数据
@property (strong,nonatomic) NSMutableArray *helps;
重写helps的get方法来加载数据,加载出来的数值中存储的都是DYHelp模型
这个方法的第二个参数的意思简单解释一下
NSArray *helpArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
/*
NSJSONReadingMutableContainers = (1UL << 0), 容器是可变的
NSJSONReadingMutableLeaves = (1UL << 1), 叶子是可变的
NSJSONReadingAllowFragments = (1UL << 2) 允许顶级节点既不是数组也不是字典
*/
#pragma mark - 懒加载数据
-(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;
}
四、小优化
这样数据的加载基本完成,但是这样加载有一点缺陷,当加载数据变化的时候我们就要到控制器里面来修改控制器的懒加载,这样不符合面向对象的编程思想,程序的可维护性不高,那我们该怎么做呢?
这里我们在模型里面封装一个方法,在这个方法里面进行加载数据和将数据转换成模型的操作,返回一个存储模型的数组,这样当加载数据的方式发生改变的时候就可以直接修改模型里面的加载数据的方法,只要返回一个模型数组就好了。
改进代码如下:
//模型对象中的方法声明
+ (NSMutableArray *)getModelArray;
//模型对象中的方法实现
+ (NSMutableArray *)getModelArray{
//得到文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"json"];
//json文件使用data来读取
NSData *data = [NSData dataWithContentsOfFile:path];
/*方法参数的简单描述
NSJSONReadingMutableContainers = (1UL << 0), 容器是可变的
NSJSONReadingMutableLeaves = (1UL << 1), 叶子是可变的
NSJSONReadingAllowFragments = (1UL << 2) 允许顶级节点既不是数组也不是字典
*/
//此方法通过data返回一个数组
NSArray *helpArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
//创建一个模型数组
NSMutableArray *modelArray = [NSMutableArray new];
for (NSDictionary *dic in helpArray) {
//字典转模型并存入模型数组中
DYHelp *help = [DYHelp helpWithDic:dic];
[modelArray addObject:help];
}
return modelArray;
}
这样的话就可以懒加载的方法就可以简化为
-(NSArray *)helps{
if (_helps == nil) {
_helps = [DYHelp getModelArray];
}
return _helps;
}

浙公网安备 33010602011771号