Creating a Dictionary from a PLIST in an NSString

431 views Asked by At

So, I've written this convenience method as a solution to the problem where creating an NSDictionary from a Property List isn't as easy as from a file/web resource. While I could parse this using NSXMLParser, or change the way my server sends data (json, whatever), that isn't my concern at the moment, rather my concern is that this method is undeniably slow and prone to collision since I don't use rand() to determine the .plist's file name.

So, is it possible to make this faster? I need to ensure that the file doesn't subsist between deletions of the app, hence the use of /tmp.

+ (NSDictionary *)dictionaryFromPropertyListString:(NSString *)stringToParse
{
    NSLog(@"dictionaryFromPropertyListString: %@", stringToParse);

    NSString *newFilePath = [NSTemporaryDirectory() stringByAppendingString:@"/Stuff.plist"];

    [stringToParse writeToFile:newFilePath atomically:YES encoding:NSASCIIStringEncoding error:nil];

    NSDictionary *dictionaryToReturn = [NSDictionary dictionaryWithContentsOfFile:newFilePath];

    NSLog(@"returned dictionary: %@", [dictionaryToReturn descriptionInStringsFileFormat]);

    return dictionaryToReturn;
}
1

There are 1 answers

0
Max On BEST ANSWER
NSString *error;
NSPropertyListFormat format;
NSDictionary* plist = [NSPropertyListSerialization propertyListFromData: [yourString dataUsingEncoding:NSUTF8StringEncoding] mutabilityOption: NSPropertyListImmutable format: &format errorDescription:&error];