Json的本地寫入和讀取,也可以方便在開發中資料的調試

來源:互聯網
上載者:User

標籤: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的本地寫入和讀取,也可以方便在開發中資料的調試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.