Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在循环中将对象添加到字典会覆盖以前的值_Ios_Cocoa Touch_Nsarray_Nsdictionary - Fatal编程技术网

Ios 在循环中将对象添加到字典会覆盖以前的值

Ios 在循环中将对象添加到字典会覆盖以前的值,ios,cocoa-touch,nsarray,nsdictionary,Ios,Cocoa Touch,Nsarray,Nsdictionary,我有三个NSArrays,我想把它们合并成一个NSDictionary。问题是,当我遍历数组并创建字典时,它会覆盖前面的对象。最后我的字典里只有一个宾语。我做错了什么?这是我的密码: NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; for(int i=0; i<[array0 count]; i++) { [dict setObject:[array0 objectAtIndex:i]

我有三个
NSArray
s,我想把它们合并成一个
NSDictionary
。问题是,当我遍历数组并创建字典时,它会覆盖前面的对象。最后我的字典里只有一个宾语。我做错了什么?这是我的密码:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(int i=0; i<[array0 count]; i++) {
    [dict setObject:[array0 objectAtIndex:i] 
             forKey:@"one"];
    [dict setObject:[array1 objectAtIndex:i] f
              orKey:@"two"];
    [dict setObject:[array2 objectAtIndex:i] 
             forKey:@"three"];
}

谢谢

在循环的每次迭代中,您都在重复使用键
(“一”、“二”和“三”)
NSDictionary中的键必须是唯一的。

问题 在特定的关键点插入和替换同一对象。所以字典的所有内容都是它在最后一个索引中的最后一个对象

解决方案 使用此代码可以使用特定键将三个数组添加到一个字典中

NSDictionary *yourDictinary = @{@"one": array0, @"two": array1, @"three": array3};
编辑 如果您需要将
nsmutablearray
的对象添加到一个
NSDictionary
中,您可以按照@ElJay发布的答案进行操作,但这不是一个好的做法,因为您要处理具有唯一键的多个对象

更新 为此,我们讨论的是单个NSMutableArray和多个NSDictionary

请遵循以下代码:

NSMutableArray *allObjects = [NSMutableArray new];
for(int i=0; i<[array0 count]; i++) {
    dict = @{@"one": array0[i], @"two": array1[i], @"three": array2[i]};
    [allObjects addObject:dict];
}
NSMutableArray*allObjects=[NSMutableArray new];
对于(int i=0;i这里是:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(int i=0; i<[array0 count]; i++) {
    [dict setObject:[array0 objectAtIndex:i] forKey:[NSString stringWithFormat:@"arr0_%d", i]];
    [dict setObject:[array1 objectAtIndex:i] forKey:[NSString stringWithFormat:@"arr1_%d", i]];
    [dict setObject:[array2 objectAtIndex:i] forKey:[NSString stringWithFormat:@"arr2_%d", i]];
}
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];

对于(int i=0;i如果需要多个字典但只有三个键,则应将每个dict保存在一个数组中。

正如数组中每个索引有一个对象一样,字典中每个键有一个对象。您使用的键是
@“一”
@“两”
@“三”
在循环的每次迭代中,因此会重写字典为每个键存储的一个对象。回到数组类比,这相当于每次通过循环总是分配给数组的相同索引,从而覆盖以前在同一索引中的内容。旁注:
[NSString stringWithFormat:@“没有格式说明符的字符串”]
没有太多意义。直接使用文本字符串即可。请参阅下面的修订答案这段代码为我做了这件事。
code
[allDevices addObjectsFromArray:[NSArray arrayWithObjects:[NSMutableDictionary Dictionary WithObjects:[NSArray arrayWithObjects:rowID,name,company,nil]forKeys:[NSArray arrayWithObjects:@“rowID”,“name”,“name”,“company”,nil]],nil]]
code
这不是用户要做的事情。他想将所有对象合并到一个字典中,而不是将3个数组合并到一个字典中。显然,用户正在以一种糟糕的方式处理hi问题。我不知道他想做什么。@MatteoGobbi查看OP,他想将3个数组添加到一个字典中!他没有说他想添加同一个NSDictionary中的所有对象。这对特定键没有意义。他的努力表明@MatteoGobbi是正确的。他似乎想要所有的对象(无论听起来多么奇怪)@ElJay我一直在关注他的问题,但如果我误解了他的意思,我也会编辑这个问题,但这不是一个好方法(我不知道你打算把所有对象添加到一个字典中)@d、 barrett000您也可以创建JSON阵列,但实际上…这并没有提供我所需要的。谢谢您的尝试。这个问题没问题,但请提供更多信息,因为这就是您获得否决票的原因!
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for(int i=0; i<[array0 count]; i++) {
    [dict setObject:[array0 objectAtIndex:i] forKey:[NSString stringWithFormat:@"arr0_%d", i]];
    [dict setObject:[array1 objectAtIndex:i] forKey:[NSString stringWithFormat:@"arr1_%d", i]];
    [dict setObject:[array2 objectAtIndex:i] forKey:[NSString stringWithFormat:@"arr2_%d", i]];
}
self.array0 = @[@"Array0_0",@"Array0_1",@"Array0_2", @"Array0_3"];
self.array1 = @[@"Array1_0",@"Array1_1",@"Array1_2", @"Array1_3"];
self.array2 = @[@"Array2_0",@"Array2_1",@"Array2_2", @"Array2_3"];

NSMutableArray *finalArray = [[NSMutableArray alloc] init];

for (int i=0; i< [_array0 count]; i++) {

    NSDictionary *dict = @{@"one":[_array0 objectAtIndex:i], @"two":[_array1 objectAtIndex:i],@"three":[_array2 objectAtIndex:i]};
    [finalArray addObject:dict];
}

NSLog(@"finalArray = %@", [finalArray description]);