NSMutableArray在for循环中被覆盖

时间:2014-03-06 10:01:36

标签: objective-c if-statement for-loop nsmutablearray nsdictionary

我四处搜索并认为我找到了答案,但似乎没有起作用。我正在尝试创建一个字典数组以供以后排序等。我可以将所有正确的数据放入字典对象和密钥对中,但是当我尝试将字典添加到数组时,先前的数组条目被“覆盖”。我意识到我对NSArray的指针做错了,但是我无法理解。一篇文章建议将数组的实例化移出for循环,但以下两个片段产生相同的结果。

NSMutableDictionary *tempMatricesNW = [NSMutableDictionary dictionary];

NSUInteger middle = 0;

if (([factorsNW count] % 2) != 0) {
    middle = (([factorsNW count]+1)/2)-1;
}

NSMutableArray *tempMatriceArrayNW = [NSMutableArray array];

for (NSUInteger i = 1; i < [factorsNW count]-1; i++) {

    if (i == middle) {
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"x"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"y"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]-[[factorsNW objectAtIndex:i] intValue]] forKey:@"diff"];
        [tempMatriceArrayNW addObject:tempMatricesNW];
    }
    else {
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"x"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:(([factorsNW count]-i)-1)] intValue]] forKey:@"y"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:abs([[factorsNW objectAtIndex:i] intValue] - [[factorsNW objectAtIndex:(([factorsNW count]-i)-1)] intValue])] forKey:@"diff"];
        [tempMatriceArrayNW addObject:tempMatricesNW];
    }

    NSLog(@"%@", tempMatriceArrayNW);

}

我得到了同样的结果......

NSMutableDictionary *tempMatricesNW = [NSMutableDictionary dictionary];

NSUInteger middle = 0;

if (([factorsNW count] % 2) != 0) {
    middle = (([factorsNW count]+1)/2)-1;
}

for (NSUInteger i = 1; i < [factorsNW count]-1; i++) {

    NSMutableArray *tempMatriceArrayNW = [NSMutableArray array];

    if (i == middle) {
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"x"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"y"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]-[[factorsNW objectAtIndex:i] intValue]] forKey:@"diff"];
        [tempMatriceArrayNW addObject:tempMatricesNW];
    }
    else {
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"x"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:(([factorsNW count]-i)-1)] intValue]] forKey:@"y"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:abs([[factorsNW objectAtIndex:i] intValue] - [[factorsNW objectAtIndex:(([factorsNW count]-i)-1)] intValue])] forKey:@"diff"];
        [tempMatriceArrayNW addObject:tempMatricesNW];
    }

    NSLog(@"%@", tempMatriceArrayNW);

}

我把NSMutableArray的实例化放在哪里似乎并不重要所以我必须做错其他的事情......

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您之前的条目被覆盖,因为您使用相同的字典,因此您指向相同的内存分配。您需要在for或after的每个循环中创建一个新的字典,以便将tempMatricesNW字典添加到数组中,再次实例化

NSUInteger middle = 0;

if (([factorsNW count] % 2) != 0) {
    middle = (([factorsNW count]+1)/2)-1;
}

NSMutableArray *tempMatriceArrayNW = [NSMutableArray array];

for (NSUInteger i = 1; i < [factorsNW count]-1; i++) {

    NSMutableDictionary *tempMatricesNW = [NSMutableDictionary dictionary];

    if (i == middle) {
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"x"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"y"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]-[[factorsNW objectAtIndex:i] intValue]] forKey:@"diff"];
        [tempMatriceArrayNW addObject:tempMatricesNW];
    }
    else {
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:i] intValue]] forKey:@"x"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:[[factorsNW objectAtIndex:(([factorsNW count]-i)-1)] intValue]] forKey:@"y"];
        [tempMatricesNW setObject:[NSNumber numberWithInt:abs([[factorsNW objectAtIndex:i] intValue] - [[factorsNW objectAtIndex:(([factorsNW count]-i)-1)] intValue])] forKey:@"diff"];
        [tempMatriceArrayNW addObject:tempMatricesNW];
    }

    NSLog(@"%@", tempMatriceArrayNW);

}