Objective-C 奇怪的 plist 到字典的行为

发布于 2024-11-13 04:50:53 字数 744 浏览 2 评论 0 原文

我有一个非常简单的 plist,它被加载到 NSDictionary 中。但是,当我尝试访问特定值时,没有可用数据。

这就是我的 plist 的结构:

edit* xml 搞乱了。你可以去pastebin看看: http://pastebin.com/C419ZVeJ

这里我加载plist:(

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [bundlePath stringByAppendingPathComponent:@"Test.plist"];
NSDictionary *metaPlistData = [NSDictionary dictionaryWithContentsOfFile:finalPath];


NSDictionary *meta = [metaPlistData valueForKey:@"meta"];
NSDictionary *assets = [meta valueForKey:@"sd"];

我已经删除了访问key=1 条目)

当我检查 gdb 中的“meta”字典和“assets”字典时,“meta”包含所需的条目。然而,资产始终为零。我真的在这里迷失了..有什么想法吗?我在代码的其他位置使用这种方法从 plist 加载数据,这从来都不是问题。

i have a really simple plist which is loaded into a NSDictionary. However, when i try to access a specific value, no data is available.

This is how my plist is structured:

edit* xml got messed up. You can take a look at it at pastebin:
http://pastebin.com/C419ZVeJ

Here i load the plist:

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [bundlePath stringByAppendingPathComponent:@"Test.plist"];
NSDictionary *metaPlistData = [NSDictionary dictionaryWithContentsOfFile:finalPath];


NSDictionary *meta = [metaPlistData valueForKey:@"meta"];
NSDictionary *assets = [meta valueForKey:@"sd"];

(i have removed the line where i access the key=1 entry)

When i inspect the "meta" dictionary and the "assets" dictionary in gdb, "meta" contains the required entries. However, assets always is nil. I am really lost here.. any ideas why? I load data from plists using this approach at other locations in my code and it has never been a problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

挽心 2024-11-20 04:50:53

乍一看,您似乎在层次结构中落后了一层。 “sd”不是“meta”字典中的键,而是“1”字典中的键。尝试一下:

NSDictionary *meta = [metaPlistData objectForKey:@"meta"];
NSDictionary *one = [meta objectForKey:@"1"];
NSDictionary *assets = [one objectForKey:@"sd"];

还要注意,您应该使用 -objectForKey: 进行字典访问(-valueForKey: 可能会在这种情况下工作,但它属于键值编码机制这有点不同。)

(另外,不确定这是否只是一个粘贴问题,但你的 plist 看起来不完整。)

On first blush, it looks like you're off by one layer in the hierarchy. "sd" isn't a key in the "meta" dictionary, it's a key in the "1" dictionary. Try this:

NSDictionary *meta = [metaPlistData objectForKey:@"meta"];
NSDictionary *one = [meta objectForKey:@"1"];
NSDictionary *assets = [one objectForKey:@"sd"];

Note too that you should be using -objectForKey: for dictionary access (-valueForKey: will probably work in this context but it belongs to the key-value coding mechanism which is something a little different.)

(Also, not sure if this is just a paste issue, but your plist looks incomplete.)

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