When parsing json, many people will assign the parsed data to nsdictionary, and fetch the corresponding value by the key value.
Sometimes we make additions or deletions to the data.
In the face of deleting data under a key, the first reaction is to use a mutable dictionary to assign the parsed data to it, but in the process of using it, the program crashes after the Removeobjectforkey method is executed.
But in normal use without any mistakes, can still use other methods to take key or value, this situation is confusing. Please read the error message carefully.
View the crash information under output is
Terminate_handler unexpectedly threw an exception
Using exception capture to see where the problem lies, we put the Removeobjectforkey method in @try
@try {}@catch (nsexception *exception) { NSLog (@ "%@", exception);} @finally { NSLog (@ "end");}
The output exception is:
Object
The Removeobjectforkey method cannot be executed on an immutable array.
Originally we used a mutable dictionary to accept the parsed JSON, although it can be used normally, but there are still some problems.
It was found that the mutable dictionary was only seen as mutable on the surface, but in fact it was an immutable dictionary. We have made minor changes to the code, transforming the immutable dictionary into a mutable dictionary for the purpose of deleting the data.
+ (Instancetype) dictionarywithdictionary: (Nsdictionary *) dict;
This allows the Remove method to execute normally.
Seeing is not necessarily a real