我有一个非常简单的 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.
发布评论
评论(1)
乍一看,您似乎在层次结构中落后了一层。 “sd”不是“meta”字典中的键,而是“1”字典中的键。尝试一下:
还要注意,您应该使用
-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:
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.)