162

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?

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
Ali
  • 10,774
  • 10
  • 56
  • 83

6 Answers6

369

NSDictionary -> NSData:

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];

NSData -> NSDictionary:

NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
KIDdAe
  • 2,714
  • 2
  • 22
  • 29
Anh Nguyen
  • 5,245
  • 1
  • 20
  • 18
52

NSDictionary -> NSData:

NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:yourDictionary forKey:@"Some Key Value"];
[archiver finishEncoding];
[archiver release];

// Here, data holds the serialized version of your dictionary
// do what you need to do with it before you:
[data release];

NSData -> NSDictionary

NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];
[unarchiver finishDecoding];
[unarchiver release];
[data release];

You can do that with any class that conforms to NSCoding.

source

Brian
  • 14,610
  • 7
  • 35
  • 43
LeonS
  • 2,684
  • 2
  • 31
  • 36
46

Use NSJSONSerialization:

NSDictionary *dict;
NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

NSDictionary *dictFromData = [NSJSONSerialization JSONObjectWithData:dataFromDict
                                                             options:NSJSONReadingAllowFragments
                                                               error:&error];

The latest returns id, so its a good idea to check the returned object type after you cast (here i casted to NSDictionary).

Aleksandr B.
  • 505
  • 4
  • 6
user1244109
  • 2,166
  • 2
  • 26
  • 24
24

In Swift 2 you can do it in this way:

var dictionary: NSDictionary = ...

/* NSDictionary to NSData */
let data = NSKeyedArchiver.archivedDataWithRootObject(dictionary)

/* NSData to NSDictionary */
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! NSDictionary

In Swift 3:

/* NSDictionary to NSData */
let data = NSKeyedArchiver.archivedData(withRootObject: dictionary)

/* NSData to NSDictionary */
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObject(with: data)
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
  • Did not work ! Swift 3 equivalent code did not work. let data = NSKeyedArchiver.archivedData(withRootObject: dataFromNetwork) [_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance ... – mythicalcoder Nov 03 '16 at 14:46
  • Appreciate your quick response. What I meant is this - Swift 3 code also did not work. My data is from 3lvis/networking HTTP library, it is of `Any?` type. But it showed error converting from `NSDictionary` to `NSData`. So I tried your code. It did not work. – mythicalcoder Nov 03 '16 at 15:50
19

NSDictionary from NSData

http://www.cocoanetics.com/2009/09/nsdictionary-from-nsdata/

NSDictionary to NSData

You can use NSPropertyListSerialization class for that. Have a look at its method:

+ (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format
                              errorDescription:(NSString **)errorString

Returns an NSData object containing a given property list in a specified format.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
0

Please try this one

NSError *error;
NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:webData
                                options:NSJSONReadingMutableContainers
                                error:&error];
Alex Zavatone
  • 4,106
  • 36
  • 54
Vijay Kachhadiya
  • 366
  • 2
  • 11