Why doesen't the number 2 work in this for-loop?

Posted by Emil on Stack Overflow See other posts from Stack Overflow or by Emil
Published on 2010-04-24T21:26:56Z Indexed on 2010/04/24 21:33 UTC
Read the original article Hit count: 239

Filed under:
|
|

Hello.

I have a function that runs trough each element in an array. It's hard to explain, so I'll just paste in the code here:

NSLog(@"%@", arraySub);

for (NSString *string in arrayFav){ 

    int favoriteLoop = [string intValue] + favCount;
    NSLog(@"%d", favoriteLoop);

    id arrayFavObject = [array objectAtIndex:favoriteLoop];
    [arrayFavObject retain];
    [array removeObjectAtIndex:favoriteLoop];
    [array insertObject:arrayFavObject atIndex:0];
    [arrayFavObject release];

    id arraySubFavObject = [arraySub objectAtIndex:favoriteLoop];
    [arraySubFavObject retain];
    [arraySub removeObjectAtIndex:favoriteLoop];
    [arraySub insertObject:arraySubFavObject atIndex:0];
    [arraySubFavObject release];

    id arrayLengthFavObject = [arrayLength objectAtIndex:favoriteLoop];
    [arrayLengthFavObject retain];
    [arrayLength removeObjectAtIndex:favoriteLoop];
    [arrayLength insertObject:arrayLengthFavObject atIndex:0];
    [arrayLengthFavObject release];

}

NSLog(@"%@", arraySub);

The array arrayFav contains these strings: "3", "8", "2", "10", "40". Array array contains 92 strings with a name. Array arraySub contains numbers 0 to 91, representing a filename with a title from the array array. Array arrayLength contains 92 strings representing the size of each file from array arraySub.

Now, the first NSLog shows, as expected, the numbers 0 to 91. The NSLog-s in the loop shows the numbers 3, 8, 2, 10, 40, also as expected.

But here's the odd part: the last NSLog shows these numbers: 40, 10, 0, 8, 3, 1, 2, 4, 5, 6, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91

that is 40, 10, 0, 8, 3, and so on.

It was not supposed to be a zero in there, it was supposed to be a 2..

Do you have any idea at why this is happening or a way to fix it? Thank you.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about for-loop