Nsdictionary converted to NSData

Source: Internet
Author: User

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

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.