【发布时间】:2015-06-27 12:02:49
【问题描述】:
我已经查看了 Parse Plist (NSString) into NSDictionary 并认为它不是重复的,因为该问题及其答案并未解决我的担忧。
我在文件系统中有一个.plist 文件,其结构如下:
这个.plist文件的源代码如下:
{
"My App" = {
"Side Panel" = {
Items = {
Price = "#123ABC";
};
};
};
}
我知道如何像这样在Root 中获取项目:
[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSString value = [dict objectForKey:@"key"]);
但是如果结构和我的一样,带有分层字典怎么办? 如何获得Price 的值?
我想用一种方法完成这一切,最好是这样:
打电话
NSString *hexString = [self getColorForKey:@"My App.Side Panel.Items.Price"];
定义
- (NSString *) getColorForKey: (NSString *)key
{
NSArray *path = [key componentsSeparatedByString:@"."];
NSDictionary *colors = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Colors" ofType:@"plist"]];
NSString *color = @"#FFFFFF"; // white is our backup
// What do I put here to get the color?
return color;
}
【问题讨论】:
-
@LouisTur 它处理的是从内存中的字符串解析出来的,我看不出它或其答案如何适用于我的问题。
-
@LouisTur 是的,这个答案对我一点帮助都没有。你能详细说明你认为它是如何相关的吗?也许更多地了解你的思维过程会有所帮助。
-
您是在问如何到达
NSDictionary中的子节点吗?它在概念上与获取根节点NSString *price = dict[@"Root"][@"MyApp"][@"Side Panel"][@"Items"][@"Price"]相同。使用 KVO 也是一种可能。遍历字典是一个基本概念,无论您的数据源如何,我都会在发布之前快速搜索该主题。但这可能会有所帮助:appventure.me/2011/12/07/… -
@LouisTur 我添加了一个示例案例,说明我想如何做到这一点。这是否有助于您理解我的问题?
标签: ios dictionary tree plist