Previously wrote a method of nsdictionary conversion to NSData as follows
nsmutabledata *data = [[nsmutabledata alloc] init];
nskeyedarchiver *archiver = [[nskeyedarchiver alloc] Initforwritingwithmutabledata:d ATA];
[Archiver encodeobject: Messagedic forkey:@ "KeyValue"];
[Archiver finishencoding];
Need to set a key to archive, to get the original dictionary with the same key file
nskeyedunarchiver *unarchiver = [[nskeyedunarchiver alloc] Initforreadingwithdata:d ATA];
nsdictionary *mydictionary = [Unarchiver decodeobjectforkey:@ "KeyValue"];
[Unarchiver finishdecoding];
However, setting the key access value, if concurrent, will cause data confusion, if you set a different key, the solution is cumbersome to use which key.
Then on GitHub found a third-party library Jsonkit, there is only one class, can complete all types of data between the conversion, very practical, now the project I also use this library, import header file #import "JSONKit.h "You need to be aware that he is using non-arc.
The code Nsdictionary converted to NSData is as follows:
NSData *data = [dic jsondata];
The code NSData converted to Nsdictionary is as follows:
Nsdictionary *mydictionary = [data objectfromjsondata];
Other types of data are similar to NSData, so look at the API, which is as simple as that.
Nsdictionary converted to NSData