从 NSString 中的 PLIST 创建字典
因此,我编写了这个方便的方法来解决从属性列表创建 NSDictionary 不像从文件/Web 资源创建 NSDictionary 那样容易的问题。虽然我可以使用 NSXMLParser 解析它,或者更改服务器发送数据(json 等)的方式,但这不是我目前关心的问题,而是我关心的是这种方法无疑很慢并且由于我不使用 rand() 来确定 .plist 的文件名,因此容易发生冲突。
那么,有可能让它更快吗?我需要确保该文件在应用程序删除之间不存在,因此使用 /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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)