views:

59

answers:

1

A web service provides data in the form of a plist. After the download, I have all of the data in an NSData object, which I want to convert to an NSDictionary. Right now the only way I know of to do that without parsing it by hand (yuck) is this:

static NSString *fileName = @"tempFile";
[data writeToFile: fileName atomically: NO];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: fileName];

I'm surprised NSDictionary doesn't have a dictionaryWithData: method. I wouldn't even mind converting the NSData to an NSString first, if dictionaryWithString: existed. Clearly the SDK is doing something similar under the hood, but it isn't exposed to developers.

Can anyone suggest a better way to do this? One of the files I need to convert can potentially be pretty big, and doing the write-to-file+read-from-file operations could be a bit slow.

+2  A: 

Use one of the following:

+[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:]
+[NSPropertyListSerialization propertyListWithData:options:format:error:]

The former is available in iOS 2.0+ but has been deprecated in 4.0.

Ole Begemann
Excellent! That is exactly what I needed.
Amagrammer