Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
Objective c NSMutableArray覆盖最后一个对象_Objective C - Fatal编程技术网

Objective c NSMutableArray覆盖最后一个对象

Objective c NSMutableArray覆盖最后一个对象,objective-c,Objective C,我在循环外初始化了nsmutablearray和nsmutable dictinay,而不是将最后一个对象覆盖到所有数组 NSMutableDictionary *dictNew=[[NSMutableDictionary alloc]init]; NSMutableArray *newArr =[[NSMutableArray alloc]init]; //dictNew=[[NSMutableDictionary alloc]init]; for (int i=0; i<[[[con

我在循环外初始化了nsmutablearray和nsmutable dictinay,而不是将最后一个对象覆盖到所有数组

NSMutableDictionary *dictNew=[[NSMutableDictionary alloc]init];
NSMutableArray *newArr =[[NSMutableArray alloc]init];
//dictNew=[[NSMutableDictionary alloc]init];

for (int i=0; i<[[[contactsData valueForKey:@"firstName"] objectAtIndex:0] count]; i++)
{
    [dictNew setObject:[[[contactsData valueForKey:@"firstName"]objectAtIndex:0]objectAtIndex:i] forKey:@"firstName"]:
    [dictNew setObject:[[[contactsData valueForKey:@"lastName"]objectAtIndex:0]objectAtIndex:i] forKey:@"lastName"];
    [dictNew setObject:[[[contactsData valueForKey:@"phones"]objectAtIndex:0]objectAtIndex:i] forKey:@"phones"];
    NSLog(@"%@",dictNew);
    [newArr addObject:dictNew];
    NSLog(@"newarr %@",newArr);
}
NSMutableDictionary*dictNew=[[NSMutableDictionary alloc]init];
NSMutableArray*newArr=[[NSMutableArray alloc]init];
//dictNew=[[NSMutableDictionary alloc]init];

对于(int i=0;i你不能像那样重复使用
dictNew
。你每次迭代都需要一个新的实例。当你拥有它时,你会一次又一次地将同一个字典添加到数组中

试试这个:

NSMutableArray *newArr = [[NSMutableArray alloc]init];
for (int i = 0; i < [[[contactsData valueForKey:@"firstName"] objectAtIndex:0] count]; i++) {
    NSMutableDictionary *dictNew=[[NSMutableDictionary alloc]init];
    [dictNew setObject:[[[contactsData valueForKey:@"firstName"]objectAtIndex:0]objectAtIndex:i] forKey:@"firstName"]:
    [dictNew setObject:[[[contactsData valueForKey:@"lastName"]objectAtIndex:0]objectAtIndex:i] forKey:@"lastName"];
    [dictNew setObject:[[[contactsData valueForKey:@"phones"]objectAtIndex:0]objectAtIndex:i] forKey:@"phones"];
    NSLog(@"%@",dictNew);
    [newArr addObject:dictNew];
    NSLog(@"newarr %@",newArr);
}
NSMutableArray*newArr=[[NSMutableArray alloc]init];
对于(int i=0;i<[[contactsdatavalueforkey:@“firstName”]objectAtIndex:0]计数];i++){
NSMutableDictionary*dictNew=[[NSMutableDictionary alloc]init];
[dictNew setObject:[[contactsData valueForKey:@“firstName”]objectAtIndex:0]objectAtIndex:i]forKey:@“firstName”]:
[dictNew setObject:[[contactsData valueForKey:@“lastName”]objectAtIndex:0]objectAtIndex:i]forKey:@“lastName”];
[dictNew setObject:[[contactsData valueForKey:@“电话”]objectAtIndex:0]objectAtIndex:i]forKey:@“电话”];
NSLog(@“%@”,新的);
[newArr addObject:dictNew];
NSLog(@“newarr%@”,newarr);
}
我还建议进行一些小的改进和清理:

NSMutableArray *newArr = [[NSMutableArray alloc] init];
NSArray *firstNames = [[contactsData valueForKey:@"firstName"] objectAtIndex:0];
for (NSInteger i = 0; i < firstNames.count; i++) {
    NSMutableDictionary *dictNew = [[NSMutableDictionary alloc] init];
    dictNew[@"firstName"] = firstNames[i];
    dictNew[@"lastName"] = [[[contactsData valueForKey:@"lastName"] objectAtIndex:0] objectAtIndex:i];
    dictNew[@"phones"] = [[[contactsData valueForKey:@"phones"] objectAtIndex:0] objectAtIndex:i];
    NSLog(@"dictNew = %@", dictNew);
    [newArr addObject:dictNew];
}
NSLog(@"newArr = %@", newArr);
NSMutableArray*newArr=[[NSMutableArray alloc]init];
NSArray*firstNames=[[contactsData valueForKey:@“firstName”]对象索引:0];
对于(NSInteger i=0;i
请发布
NSLog()
的输出,并美化代码(缩进)。此外,如果将对内部数组的引用指定给引用变量,代码的可读性会更好。