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;
}
有帮助吗?

解决方案

NSString *error;
NSPropertyListFormat format;
NSDictionary* plist = [NSPropertyListSerialization propertyListFromData: [yourString dataUsingEncoding:NSUTF8StringEncoding] mutabilityOption: NSPropertyListImmutable format: &format errorDescription:&error];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top