并按数组顺序对应匹配项

并按数组顺序对应匹配项

本文介绍了将多个 NSArrays 添加到 NSMutableDictionary 并按数组顺序对应匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组,分别是 itemName 和 itemImages

I have two arrays say itemName and itemImages

NSArray *itemName = @[@"Blog", @"Missions", @"Subscriptions", @"Questions", @"Aide",@"Disconnect"];
NSArray *itemImages = @[@"ico-1",@"ico-2", @"ico-3", @"ico-4", @"ico-5", @"ico-6"];

我需要将它们添加到字典中

I need to add them to a dictionary

 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
    );
}

我更想要的是这样的想法:

What I rather want is some think like this :

{
  {
    itemImages = "icon-1",
    itemName = Blog
  },
  {
    itemImage = "icon-2",
    itemName = "Missions"
  }, ...
}

推荐答案

您无法使用单个 NSDictionary 获得该结构 - 您需要 NSDictionary 的 NSArray 对象.

You cannot get that structure with a single NSDictionary - you need an NSArray of NSDictionary objects.

您可以这样做:

NSMutableArray *menuItemNameImage = [NSMutableArray array];
for (int i = 0 ; i != itemNames.count ; i++) {
    [menuItemNameImage addObject: @{
        @"itemName" : itemNames[i]
    ,   @"itemImage" : itemImages[i]
    }];
}

这篇关于将多个 NSArrays 添加到 NSMutableDictionary 并按数组顺序对应匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:45