简体   繁体   English

从NSString中的PLIST创建字典

[英]Creating a Dictionary from a PLIST in an NSString

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. 因此,我写了这种方便的方法来解决以下问题:从“属性列表”创建NSDictionary并不像从文件/ Web资源创建NSDictionary 那样容易。 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. 虽然我可以使用NSXMLParser解析此文件,或者更改服务器发送数据的方式(json等),但目前我不在乎,但我担心的是,这种方法无疑是缓慢的,并且很容易发生冲突,因为我没有这样做。 t使用rand()确定.plist的文件名。

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. 我需要确保在删除应用程序之间不存在该文件,因此要使用/ 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM