views:

77

answers:

2

here is the code, where listArray is NSMutableArray
......

listArray = [[NSMutableArray alloc] initWithCapacity:100];
        for ( int i = 0 ; i < 100 ; i ++ )
            [listArray addObject:[NSNumber numberWithInt:i]];

....

for(int max=99; max >= 0 ; max --)
{
    int rN = (arc4random() % max) + 0;

    //Warning 1: initialization makes integer from pointer without a cast
    int temp = [listArray objectAtIndex:rN];
    [listArray insertObject:[listArray objectAtIndex:max] atIndex:rN];

    //passing argument 1 of 'insertObject:atIndex:' makes pointer from integer without a cast
    [listArray insertObject:temp atIndex:max];
}
A: 

It should be:

NSNumber *temp = [listArray objectAtIndex:rN];

NSMutableArray holds Obj-C objects, which is why you have to use NSNumber numberWithInt:. That means when you pull it out with objectAtIndex, you get a NSNumber *.

Matthew Flaschen
A: 

You need to "unbox" the int value:

int temp = [[listArray objectAtIndex:rN] intValue];

The array stores objects (here an NSNumber), while you want a plain int.

The same holds for insertion, that's why you call [NSNumber numberWithInt:...] while adding.

Edit: The indices in the array range from 0 ... (count-1), not sure if your +1 does what it should.

Eiko
He never actually uses the unboxed number, so I see no reason to unbox then rebox.
Matthew Flaschen
true, but the code doesn't make much sense anyway, so I thought it was more of a code example for the method invocation and warnings.
Eiko