xcode 如何在 NSDictionary 中为键和对象添加数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25909355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:36:24  来源:igfitidea点击:

How to add arrays for key and object in NSDictionary

iosobjective-carraysxcodensdictionary

提问by Muhammad Faizan

I have 2 arrays. Array AarrayA[0] contains {a,b,c,d,e...} and Array BarrayB[0] contains {a,b,c...}. And both arrays are mutable and have many objects. In NSMutableDictionaryfor each element in Array A (taken as key) should store entire Array B.

我有 2 个数组。数组 AarrayA[0] 包含 {a,b,c,d,e...},数组 BarrayB[0] 包含 {a,b,c...}。并且这两个数组都是可变的并且有很多对象。在NSMutableDictionary对于在阵列中的每个元素(作为密钥)应该存储整个阵列B.

For example:

例如:

(key value)Array A[0] have entire (object value)Array B(0...200)

(键值)数组 A[0] 具有整个(对象值)数组 B(0...200)

(key value)Array A[1] have entire (object value)Array B(0...200)

(键值)数组 A[1] 具有整个(对象值)数组 B(0...200)

(key value)Array A[2] have entire (object value)Array B(0...200)

(键值)数组 A[2] 具有整个(对象值)数组 B(0...200)

My code:

我的代码:

for (int i=0; i<100; i++)
{

    [someDictionary setObject:arrayB forKey:arrayA[i]];

}

this doesn't work and i tried this too :

这不起作用,我也试过这个:

[someDictionary initWithObjects:arrayB forKeys:arrayA[i]]

回答by Kaps18

You should use NSStringfor storing key in key-value pair. So i would recommend

您应该NSString用于将键存储在键值对中。所以我会推荐

for (int i=0; i<100; i++) {

    NSString *str=(NSString *)arrayA[i];

    [someDictionary setObject:arrayB forKey:str];

}

回答by gnasher729

Your code looks fine in principle. Obviously being fixed to 100 elements is not nice, so I'd write

您的代码原则上看起来不错。显然固定为 100 个元素并不好,所以我会写

for (id key in self.arrayA)
    [someDictionary setObject:self.arrayB forKey:key];

回答by karthikeyan

try this ansewer..i don't know brother whether i am understand your question correct or wrong

试试这个答案..我不知道兄弟我是否理解你的问题是对还是错

try my code...

试试我的代码...

NSMutableArray *tempA=[NSMutableArray array];
    NSMutableArray *tempB=[NSMutableArray array];

    for (int a=0; a<100; a++)
    {
        [tempA addObject:[NSString stringWithFormat:@"%d",a]];

    }

    NSMutableArray *dataArray=[NSMutableArray array];
    NSMutableDictionary *dict;
    for (int i=0; i<[tempA count]; i++)
    {
        for (int a=0; a<100; a++)
        {
             dict=[NSMutableDictionary dictionary];
            NSString*  localTemp=[NSString stringWithFormat:@"key-%@",[tempA objectAtIndex:i]];
            [tempB addObject:[NSString stringWithFormat:@"%d",a]];
            id object=[tempB objectAtIndex:a];
            [dict setObject:object forKey:localTemp];
            [dataArray addObject:dict];
        }
    }
    NSLog(@"dictonarry is---->%@",dataArray);

回答by user2885077

 NSArray *arrayA = [NSArray arrayWithObjects:@"1",@"2", nil];
    NSArray *arrayB = [NSArray arrayWithObjects:@"3",@"4", nil];
    NSMutableDictionary *temp = [[NSMutableDictionary alloc]init];

    for(int i =0 ; i<[arrayA count];i++)
    {
        [temp setObject:arrayB forKey:[arrayA objectAtIndex:i]];
    }