开发者

Objective-C strange plist to dictionary behavior

开发者 https://www.devze.com 2023-03-10 13:01 出处:网络
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.

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 tak开发者_开发技巧e 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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消