我有两个数组,分别是itemName和itemImages
NSArray *itemName = @[@"Blog", @"Missions", @"Subscriptions", @"Questions", @"Aide",@"Disconnect"];
NSArray *itemImages = @[@"ico-1",@"ico-2", @"ico-3", @"ico-4", @"ico-5", @"ico-6"];
我需要将它们添加到字典中
NSMutableDictionary *menuItemNameImage = [[NSMutableDictionary alloc] init];
[menuItemNameImage setValue:itemName forKey:@"itemName"];
[menuItemNameImage setValue:itemImages forKey:@"itemImages"];
NSLog(@"%@", menuItemNameImage);
但这使以下字典
{
itemImages = (
"ico-1",
"ico-2",
"ico-3",
"ico-4",
"ico-5",
"ico-6"
);
itemName = (
Blog,
Missions,
Subscriptions,
Questions,
Aide,
Disconnect
);
}
我宁愿想要这样的想法:
{
{
itemImages = "icon-1",
itemName = Blog
},
{
itemImage = "icon-2",
itemName = "Missions"
}, ...
}
最佳答案
您不能仅凭一个NSDictionary
来获得该结构-您需要一个NSArray
对象的NSDictionary
。
这是您可以执行的操作:
NSMutableArray *menuItemNameImage = [NSMutableArray array];
for (int i = 0 ; i != itemNames.count ; i++) {
[menuItemNameImage addObject: @{
@"itemName" : itemNames[i]
, @"itemImage" : itemImages[i]
}];
}
关于ios - 将多个NSArray与数组中的相应匹配项添加到NSMutableDictionary中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18501422/