Adding Multiple NSArrays to NSMutableDictionary with corresponding matching item in array order

て烟熏妆下的殇ゞ 提交于 2019-12-24 00:32:15

问题


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

But this makes the following Dictionary

{
    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"
  }, ...
}

回答1:


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

Here is how you can do it:

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



回答2:


What you could use is a NSArray of NSDictionaries. Each dictionary would have two entries: an itemImage and itemName.

It would be something like this:

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

NSMutableArray *menuItemNameImage = [NSMutableArray new];

for (NSInteger i = 0; i < itemName.count; i++) {
    NSMutableDictionary *itemNameImage = [NSMutableDictionary new];

    itemNameImage[@"itemName"] = itemName[i];

    //just a precaution if the array os images is smaller than the ones of names, to avoid unwanted crashes
    if (i < itemImages.count) { 
        itemNameImage[@"itemImage"] = itemImages[i];
    }

    [menuItemNameImage addObject:itemNameImage];
}

NSLog(@"%@", menuItemNameImage);

The output, as you want:

(
    {
        itemImage = "ico-1";
        itemName = Blog;
    },
    {
        itemImage = "ico-2";
        itemName = Missions;
    },
    {
        itemImage = "ico-3";
        itemName = Subscriptions;
    },
    {
        itemImage = "ico-4";
        itemName = Questions;
    },
    {
        itemImage = "ico-5";
        itemName = Aide;
    },
    {
        itemImage = "ico-6";
        itemName = Disconnect;
    }
)

You can access the elements like this, for example:

NSString *firstItemName = menuItemNameImage[0][@"itemName"]; 
NSString *firstItemImage = menuItemNameImage[0][@"itemImage"]; 


来源:https://stackoverflow.com/questions/18501422/adding-multiple-nsarrays-to-nsmutabledictionary-with-corresponding-matching-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!