Local write and read of JSON, also facilitates debugging of data in development

Source: Internet
Author: User

Do not know the small partners, in the development, the process of data debugging, especially in the case of a lot of state debugging, is not always troublesome to the background of the small brother change the state, always feel so much trouble,

Then you can write the data to the local, then go to the sandbox, find the write file, and go directly to change the data in this file. Then you can no longer request the network, you can directly read the file data to debug their own interface,

YXJsonSaveOrLoadTool.h

@interface yxjsonsaveorloadtool:nsobject+ (instancetype) sharetool;//** write Data */-(void) Savejsonwith: (ID) dict Urltype :(NSString *) urltype;//** read Data */-(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;}        * * Write Data */-(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]; }//** Read Data */-(nsmutabledictionary*) Loadjsonformfile: (NSString *) urltype{nsstring * Path = [self PATHFORDATAFILE:URLT    YPE];    Nsmutabledictionary * Rootobject = [[Nsmutabledictionary alloc] initwithcontentsoffile:path];    if (Rootobject = = nil) {rootobject = [[Nsmutabledictionary alloc] initwithcapacity:0]; } return rootobject;} * * Path */-(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

If there is any wrong place, please do not hesitate to advise,??

Local write and read of JSON, also facilitates debugging of data in development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.