更改 NSDictionary 的结构
Changing the structure of an NSDictionary
我有一个包含这样字典的数组:
这是我的代码:objs 的键是 "ABC"、"SKY",键是 "T-4112-561-560.ts"、"T-4112-561-561.ts"
array = [NSMutableArray array];
[array addObjectsFromArray: keys];
[array addObjectsFromArray: objs];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects: keys forKeys: objs];
[myArray addObject:[dict copy]];
这是我的输出:
2015-09-07 10:25:21.329 XBMC Remote[1082:454537] myArray:
(
{
ABC = "T-4112-561-560.ts";
},
{
SKY = "T-4112-561-561.ts";
}
)
我需要得到这样的输出:
(
{
Name=ABC;
value= "T-4112-561-560.ts";
},
{
Name=SKY;
value = "T-4112-561-561.ts";
}
)
有很多方法可以实现这一点,但基本上所有方法都归结为遍历数组,提取字典值。您可以使用许多方便的方法,例如 enumerateObjectsUsingBlock
但为了保持简单,以下代码使用简单的 for
枚举循环 -
NSMutableArray *outputArray=[NSMutableArray new];
for (NSDictionary *dict in myArray) {
NSMutableDictionary *outputDictionary=[NSMutableDictionary new];
for (NSString *key in dict.allKeys) {
NSString *value=dict[key];
outputDictionary[@"name"]=key;
outputDictionary[@"value"]=value;
}
[outputArray addObject:outputDictionary];
}
我有一个包含这样字典的数组:
这是我的代码:objs 的键是 "ABC"、"SKY",键是 "T-4112-561-560.ts"、"T-4112-561-561.ts"
array = [NSMutableArray array];
[array addObjectsFromArray: keys];
[array addObjectsFromArray: objs];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects: keys forKeys: objs];
[myArray addObject:[dict copy]];
这是我的输出:
2015-09-07 10:25:21.329 XBMC Remote[1082:454537] myArray:
(
{
ABC = "T-4112-561-560.ts";
},
{
SKY = "T-4112-561-561.ts";
}
)
我需要得到这样的输出:
(
{
Name=ABC;
value= "T-4112-561-560.ts";
},
{
Name=SKY;
value = "T-4112-561-561.ts";
}
)
有很多方法可以实现这一点,但基本上所有方法都归结为遍历数组,提取字典值。您可以使用许多方便的方法,例如 enumerateObjectsUsingBlock
但为了保持简单,以下代码使用简单的 for
枚举循环 -
NSMutableArray *outputArray=[NSMutableArray new];
for (NSDictionary *dict in myArray) {
NSMutableDictionary *outputDictionary=[NSMutableDictionary new];
for (NSString *key in dict.allKeys) {
NSString *value=dict[key];
outputDictionary[@"name"]=key;
outputDictionary[@"value"]=value;
}
[outputArray addObject:outputDictionary];
}