不知道小伙伴们,在开发中,数据调试的过程中,尤其是很多状态的情况下调试,是不是总是麻烦后台的小哥改变不同的状态,总感觉这样太麻烦了,
那么就可以,把数据写入到本地,然后去沙盒中,找到这个写入的文件,直接去改变这个文件中的数据。之后就可以不用再网络请求了,可以直接读取这个文件的数据来调试自己的界面了,
YXJsonSaveOrLoadTool.h
@interface YXJsonSaveOrLoadTool : NSObject +(instancetype)shareTool; //** 写入数据 */ -(void)SaveJsonWith:(id)dict UrlType:(NSString *)urlType; //** 读取数据 */ -(NSMutableDictionary*)loadJsonFormFile:(NSString *)urlType; @end
YXJsonSaveOrLoadTool.m
@implementation YXJsonSaveOrLoadTool +(instancetype)shareTool{ static dispatch_once_t onceToken; static YXJsonSaveOrLoadTool *instance; dispatch_once(&onceToken, ^{ instance = [[YXJsonSaveOrLoadTool alloc]init]; }); return instance; } //** 写入数据 */ -(void)SaveJsonWith:(id)dict UrlType:(NSString *)urlType{ if (dict == nil || dict == [NSNull null]) { return; } if (urlType.length==0) { urlType = @"test"; } NSString * path = [self pathForDataFile:urlType]; [dict writeToFile:path atomically:YES]; } //** 读取数据 */ -(NSMutableDictionary*)loadJsonFormFile:(NSString *)urlType { NSString * path = [self pathForDataFile:urlType]; NSMutableDictionary * rootObject = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; if (rootObject == nil ) { rootObject = [[NSMutableDictionary alloc] initWithCapacity:0]; } return rootObject; } //** 路径 */ -(NSString *)pathForDataFile:(NSString *)urltype { NSArray* documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* path = nil; if (documentDir) { path = [documentDir objectAtIndex:0]; } return [NSString stringWithFormat:@"%@/%@.%@", path,urltype,@"json"]; } @end
如果有什么错误的地方,请不惜指教,??
原文:http://www.cnblogs.com/liuwenqiang/p/6390024.html