A Dictionary of IOS Development writes to a file

Source: Internet
Author: User

In the recent development, I encountered a problem, I started not to notice the problem, some envy later maintenance of the process found a bug, this bug is about the dictionary written to the file. The reason is this, I use a dictionary to write to the file method

BOOL result = [Resultdic writetofile:citylistdatapath atomically:yes];

if (result) {

NSLog (@ "hyh-write succeeded");

}else {

NSLog (@ "hyh-write Failed");

}

As for this question, I found the answer by reading the official document, which is what the official document says:

Discussion

this method recursively validates, all the contained objects is property list objects (instances Of nsdata ,  nsdate ,  < Code class= "Code-voice" >nsnumber ,  nsstring ,  nsarray , Or nsdictionary ) before Writing out the file, and Returns no  if all the Objects is not property list objects, since the resultant file would is a valid property list.

If the dictionary ' s contents is all property list objects, the file is written by this method can is used to initialize a NE W Dictionary with the class method dictionaryWithContentsOfFile: or the instance method initWithContentsOfFile: .

This means that only the type of the attribute list type is above the red part to be correctly written to the file. Since the beginning of development, the data returned in the background is not NULL type, but now the inverse of the partial parameter value is null this causes the write file failure, then because the background is likely to change, and the dictionary write method is limited, So how do we solve this problem? One way is to let the background to do not regret null, but this way too. Well, I think the better way is to convert it to data and write to the file:

Nsdictionary *resultdic = [Data objectforkey:@ "data"];

NSData *resultdata = [nsjsonserialization datawithjsonobject:resultdic options:nsjsonwritingprettyprinted Error:nil] ;

BOOL result = [Resultdata writetofile:citylistdatapath atomically:yes];

if (result) {

NSLog (@ "hyh-write succeeded");

}else {

NSLog (@ "hyh-write Failed");

}

Analytical:

NSData *data = [NSData Datawithcontentsoffile:citylistdatapath];

Nsdictionary * Resultdic = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutablecontainers Error: NIL];

This way will be very good to avoid the back-end data on our program Impact!

A Dictionary of IOS Development writes to a file

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.