网络加载JSON数据保存在本地plist文件中

在做快递查询功能的时候需要提交一个叫快递公司编号的字段。

由于快递公司有很多,再加上每个接口提供的快递公司编号是不统一的,排除了自己手动往plist表中写数据的可能。所以只能将接口提供的JSON数据写入plist表中,需要用到时再读plist表取出来。

其中有一个问题就是:我只需要在第一次打开app时获得这些数据,将它们保存在plist文件中,以后就直接调用plist中的数据,不再调用接口获取了。

我的做法是通过另一个plist表来判断这是不是用户第一次进入app。

1.首先在启动页(广告页)调用接口加载JSON数据

- (void)getData{
...
...
        BKKCompanyDataModel *model = [BKKCompanyDataModel yy_modelWithDictionary:dic];
        NSString *home = NSHomeDirectory();
        NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
        NSString *filePath = [docPath stringByAppendingPathComponent:@"companyList.plist"];
        NSMutableArray *array = [NSMutableArray array];
        //将数据存入plist
        for(int i = 0;i<model.result.count;i++){
            BKKCompanyResultModel *Model = model.result[i];
            NSMutableDictionary *newDic = [NSMutableDictionary dictionary];
            [newDic setObject:[NSString stringWithFormat:@"%@", Model.com] forKey:@"com"];
            [newDic setObject:[NSString stringWithFormat:@"%@", Model.no] forKey:@"no"];
            [array addObject:newDic];
        }
        [array writeToFile:filePath atomically:YES];
...
...
}

字典dic是通过AFNetworking第三方库GET到的JSON数据。利用同样是第三方库的YYModel将dic转化为model。其中BKKCompanyResultModel是嵌套在BKKCompanyDataModel中的。

我将model的com字段(公司中文名称)与no字段(公司编号)写入新字典保存至plist表中。

至此,我已经将网络加载的JSON数据进行了本地持久化保存。

2.上面的方法我只需要调用一次。我们还需要做一次判断:

- (void)load{
    NSString *home = NSHomeDirectory();
    NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"timeList.plist"];
    NSArray *arrayM = [NSArray arrayWithContentsOfFile:filePath];
    if([[[arrayM objectAtIndex:0] objectForKey:@"useTime"] isEqualToString:@"1"]){
        NSLog(@"already exist data");
    }
    else{
        [self getData];
    }
}

其中timeList表中的key--useTime的value如果为1的话,说明这已经不是第一次进入app了。不再调用gatData方法。

至于timeList表的写入,在进入app后的随便一个操作中添加就行了。

3.将plist表中的数据转化为模型
plist的结构一般为

array
{
   {
     key:@"something",value:@"something"
     ...
   }
   {
     key:@"something",value:@"something"
     ...
   }
...
}

也就是有若干字典存放在数组中。
我们需要自定义一个继承于NSObject的类BKKCompanyModel
.h

#import <Foundation/Foundation.h>

@interface BKKCompanyModel : NSObject

@property(nonatomic,copy)NSString *com;
@property(nonatomic,copy)NSString *no;


-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)dataWithDict:(NSDictionary *)dict;

@end

.m

#import "BKKCompanyModel.h"

@implementation BKKCompanyModel

-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self=[super init]) {
        self.com = dict[@"com"];
        self.no = dict[@"no"];
    }
    return self;
}

+(instancetype)dataWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
@end

在我们需要用到公司编号的地方创建数组

- (NSMutableArray *)companyNo{
    if (!_companyNo) {
        _companyNo = [NSMutableArray array];
        NSString *home = NSHomeDirectory();
        NSString *docPath = [home stringByAppendingPathComponent:@"Documents"];
        NSString *filePath = [docPath stringByAppendingPathComponent:@"companyList.plist"];
        //取出数据放入数组
        NSArray  *arrayM=[NSArray arrayWithContentsOfFile:filePath];
        NSMutableArray *dictArray =[NSMutableArray array];
        //将字典放入新数组
        for(NSDictionary *dict in arrayM){
            [dictArray addObject:[BKKCompanyModel dataWithDict:dict]];
        }
        //
        for(int i=0;i<dictArray.count;i++){
            BKKCompanyModel *model = dictArray[i];
            [_companyNo addObject:model.no];
        }
    }
    return _companyNo;
}

以上,已经完成了JSON字典到plist表再到模型数组的转换。当然如果不想本地存储也行...那就得每次使用app时都要调用接口,直接把接口的JSON数据通过YYModel转化为模型数组。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 30,271评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,679评论 1 32
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,665评论 19 139
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,314评论 2 89
  • 黄花城水长城~壹基金为爱同行公益健行16公里,团队3人组合顺利完赛。壹基金,为爱同行支持的有4个儿童项目:净水计划...
    勤行乐道阅读 307评论 0 0

友情链接更多精彩内容