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