NSDictionary 来自 NSMutableData

发布于 2024-08-02 10:12:26 字数 202 浏览 2 评论 0原文

我之前使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里南柯 2024-08-09 10:12:26

您需要的是 NSPropertyListSerialization:

NSDictionary *myDict = [NSPropertyListSerialization propertyListFromData:myData mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil];

是最简单的选项。

What you need is NSPropertyListSerialization:

NSDictionary *myDict = [NSPropertyListSerialization propertyListFromData:myData mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil];

Is the simpliest option.

苍暮颜 2024-08-09 10:12:26

在不知道你到底想做什么的情况下,我将尝试一下;即使它不起作用,我相信您可以根据您的目的进行调整。

NSString *s = [[NSString alloc] initWithBytes:[mutableData bytes] length:[mutableData length] encoding:NSUTF8StringEncoding];
NSString *path = NSTemporaryDirectory();
path = [path stringByAppendingPathComponent:@"tmpfile.txt"];
if ([s writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:nil]) {
    NSDictionary *d = [[NSDictionary alloc] initWithContentsOfFile:path];
}

用简单的英语来说,根据给定的数据创建一个字符串,将其写入临时文件,然后将临时文件加载到 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.

NSString *s = [[NSString alloc] initWithBytes:[mutableData bytes] length:[mutableData length] encoding:NSUTF8StringEncoding];
NSString *path = NSTemporaryDirectory();
path = [path stringByAppendingPathComponent:@"tmpfile.txt"];
if ([s writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:nil]) {
    NSDictionary *d = [[NSDictionary alloc] initWithContentsOfFile:path];
}

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.

坏尐絯 2024-08-09 10:12:26

您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文