Rory O'Hare
Rory O'Hare

Reputation: 115

NSMutableArray being overwritten in for-loop

I searched around and thought I found the answer to this but it doesn't appear to be working. I am trying to make an array of dictionaries for later sorting, etc. I can get all the correct data into dictionary object and key pairs, but when I try to add the dictionaries to an array the previous array entries are "overwritten". I realize I'm doing something wrong with the pointer to the NSArray, but I cannot get it straight. One post suggested moving the instantiation of the array out of the for loop, but the following two snippets produce identical results.

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

}

I get the same result with this...

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

}

It doesn't seem to matter where I put the instantiation of the NSMutableArray so I must be doing something else wrong...

Thanks in advance for any help!

Upvotes: 0

Views: 116

Answers (1)

Fran Martin
Fran Martin

Reputation: 2369

Your previous entries are overwritten because you are using the same dictionary so you are pointing to the same memory allocation. You need to create a new dictionary in each loop of your for or after to add your tempMatricesNW dictionary to your array, instantiate it again

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

}

Upvotes: 1

Related Questions