標籤:patch stat ken objc 過程 doc adt atom 路徑
不知道小夥伴們,在開發中,資料調試的過程中,尤其是很多狀態的情況下調試,是不是總是麻煩背景小哥改變不同的狀態,總感覺這樣太麻煩了,
那麼就可以,把資料寫入到本地,然後去沙箱中,找到這個寫入的檔案,直接去改變這個檔案中的資料。之後就可以不用再網路請求了,可以直接讀取這個檔案的資料來調試自己的介面了,
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
如果有什麼錯誤的地方,請不惜指教,??
Json的本地寫入和讀取,也可以方便在開發中資料的調試