从 Plist 文件解析数组字典
Parsing a Dictionary of Arrays from a Plist file
我正在尝试使用特定的数据布局。我们使用按以下方式设置的 plist 文件:
根(字典):
- 1.1(数组)
-- 第 0 项(字典)
--- 标题
--- 图标
-- 第 1 项(字典)
--- 标题
--- 图标
- 1.2(数组)
-- 第 0 项(字典)
--- 标题
--- 图标
来自 plist 的这些信息被解析成一个字典,如下注销:
{
"1.0" = (
{
detail = "Detail 0";
icon = "Pull to Refresh";
title = "Item 0";
},
{
detail = "More easily refresh a subscription or playlist.";
icon = "Pull to Refresh";
title = "Pull to Refresh";
}
);
"1.1" = (
{
detail = "Create custom stations of your favorite podcasts.";
icon = Stations;
title = "Custom Stations";
}
);
}
我想知道的是子数组中的项目数。例如,有两个子数组,一个用于 1.1,一个用于 1.2。我想从这些数组中获取词典的数量?
有没有快速访问它的方法。我试图完成一个 for 循环并计算它们,但我收到 NSCFString 错误,好像我的数据不正确?
既然你已经提到自己尝试 for 循环,我不知道这是否真的是你正在寻找的那种答案,但为了这个非常具体的目的(即你确定输入词典总是有您描述的那种结构)按照这些思路应该可以解决问题:
- (NSUInteger)numberOfItemsInSubArraysOfDictionary:(NSDictionary *)dict
{
NSUInteger count = 0;
for(NSArray *array in dict.allValues) {
if ([array isKindOfClass:[NSArray class]]) {
count += array.count;
}
}
return count;
}
您可以尝试这样的操作:
for (NSString *key in [dictionary allKeys]) {
NSLog(@"Number of dictionaries for %@: %d", key, [dictionary[key] count]);
}
此循环的输出为:
Number of dictionaries for 1.0: 2
Number of dictionaries for 1.1: 1
我正在尝试使用特定的数据布局。我们使用按以下方式设置的 plist 文件:
根(字典):
- 1.1(数组)
-- 第 0 项(字典)
--- 标题
--- 图标
-- 第 1 项(字典)
--- 标题
--- 图标
- 1.2(数组)
-- 第 0 项(字典)
--- 标题
--- 图标
来自 plist 的这些信息被解析成一个字典,如下注销:
{
"1.0" = (
{
detail = "Detail 0";
icon = "Pull to Refresh";
title = "Item 0";
},
{
detail = "More easily refresh a subscription or playlist.";
icon = "Pull to Refresh";
title = "Pull to Refresh";
}
);
"1.1" = (
{
detail = "Create custom stations of your favorite podcasts.";
icon = Stations;
title = "Custom Stations";
}
);
}
我想知道的是子数组中的项目数。例如,有两个子数组,一个用于 1.1,一个用于 1.2。我想从这些数组中获取词典的数量?
有没有快速访问它的方法。我试图完成一个 for 循环并计算它们,但我收到 NSCFString 错误,好像我的数据不正确?
既然你已经提到自己尝试 for 循环,我不知道这是否真的是你正在寻找的那种答案,但为了这个非常具体的目的(即你确定输入词典总是有您描述的那种结构)按照这些思路应该可以解决问题:
- (NSUInteger)numberOfItemsInSubArraysOfDictionary:(NSDictionary *)dict
{
NSUInteger count = 0;
for(NSArray *array in dict.allValues) {
if ([array isKindOfClass:[NSArray class]]) {
count += array.count;
}
}
return count;
}
您可以尝试这样的操作:
for (NSString *key in [dictionary allKeys]) {
NSLog(@"Number of dictionaries for %@: %d", key, [dictionary[key] count]);
}
此循环的输出为:
Number of dictionaries for 1.0: 2
Number of dictionaries for 1.1: 1