NSDictionary 来自 NSMutableData
我之前使用 initWithContentsOfURL 将 plist 下载到 NSDictionary 中,但是当在同一线程上运行时,这会挂起 UI,所以现在我已经转移到 NSURLConnection ,问题是我无法再调用方法来使用 NSMutableData 初始化 NSDictionary。获取 NSMutableData 并将其放入 NSDictionary 的最佳方法是什么?
I was previously using initWithContentsOfURL to download a plist into an NSDictionary, but this hangs the UI when run on the same thread so now I have moved to NSURLConnection the issue is that I can no longer call a method to init the NSDictionary with NSMutableData. What is the best way to take NSMutableData and place it into an NSDictionary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要的是 NSPropertyListSerialization:
是最简单的选项。
What you need is NSPropertyListSerialization:
Is the simpliest option.
在不知道你到底想做什么的情况下,我将尝试一下;即使它不起作用,我相信您可以根据您的目的进行调整。
用简单的英语来说,根据给定的数据创建一个字符串,将其写入临时文件,然后将临时文件加载到 NSDictionary 对象中。
该代码远非完美(需要错误检查等内容),但它可能会帮助您入门。
警告:我已经编写 Cocoa 软件好几年了,但我从未接触过 iPhone,所以这是一个猜测。
Without knowing exactly what you're trying to do, I'm going to take a stab at this; even if it doesn't work, I'm sure you can adapt it for your purposes.
In plain English, create a string from the given data, write that out to a temp file, and then load the temp file into an
NSDictionary
object.That code is far from perfect (needs stuff like error checking, etc.) but it may get you started.
Caveat: I've been writing Cocoa software for several years, but I've never touched an iPhone, so that's a guess.
您的 NSDictionary 是否还有其他需要保留的数据?
在connection:didReceiveResponse:方法(NSUrlConnection的委托)中,你可以将旧的NSDictionary复制到NSMutableDictionary中,然后将URL响应数据插入其中。然后将 NSMutableDictionary 转换为新的 NSDictionary。
Does your NSDictionary have other data that you need to preserve?
In the connection:didReceiveResponse: method (delegate of NSUrlConnection), you can copy your old NSDictionary into a NSMutableDictionary and then insert the URL response data into it as well. Then convert the NSMutableDictionary into a new NSDictionary.