Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


geppi24geppi24 Posts: 13Registered Users
edited November 2008 in iOS SDK Development
is there an easy way to convert from NSData to NSDictionary and backwards? I want to send a NSDictionary-Object packaged into a NSData-Objekt via NSStream.

Thanks for help an iPhoneDev-Newbee!! :D
Post edited by geppi24 on


  • OssifrageOssifrage Posts: 31Registered Users
    edited November 2008
    You might want to look at JSONs - a nice representation of data as a string. The SBJSON stuff (json-framework - Google Code) looks rather excellent.
  • jeff_lamarchejeff_lamarche Posts: 420Registered Users
    edited November 2008
    Don't both with JSON for this. There's a much easier way - just archive it.
    	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];

    You can do this with any class that conforms to NSCoding, and all the collection classes do. you can then reverse the process
    		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];

    Hope that helps.
    <a href=""; target="_blank"><font color="red">Check out my iPhone Dev Blog</font></a><br />
    You can send me e-mail at my forum username at mac dot com.
  • geppi24geppi24 Posts: 13Registered Users
    edited November 2008
    Thanks guys for replying my question. The suggested Archiving-mechanism worked pretty fine for me.

    Thanks again
Sign In or Register to comment.