Ali Ali - 1 month ago 13
iOS Question

How can I convert NSDictionary to NSData and vice versa?

I am sending

NSString
and
UIImage
using bluetooth. I decided to store both in a
NSDictionary
and then convert the dictionary to
NSData
.

My question is how to convert
NSDictionary
to
NSData
and visa versa?

Answer

NSDictionary -> NSData:

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];

NSData -> NSDictionary:

NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
Comments