views:

654

answers:

3

The following line works fine in the NSLog:

NSLog(@"Selected Number: %@. Index of selected number: %i", [arrayNo objectAtIndex:row], row);

but I would like to make the index assigned to an integer variable:

int num = ([arrayNo objectAtIndex:row], row);

the line above produces an error. What I am doing wrong?

+2  A: 

An NSArray or NSMutableArray manages objects and can't handle an integer directly. The call to [arrayNo objectAtIndex:row] returns an object. To get the value as an integer you can try:

int num = [[arrayNo objectAtIndex:row] intValue];
Robert Höglund
A: 

Your example always returns 0. In the NSLog example it returns the index of where I am in the array. How can I return where I am in the array?

Charlie C
A: 

In your code, fetching the object doesn't do anything. You're just throwing it away. What you want to do is:

int num = row;

If you want to only assign the index for a specific value to num, you can do something like this (assuming array contains only NSNumbers):

int num;
for(NSNumber *n in array) {
    if ([n integerValue] == 5) {
        num = [array indexOfObject:n];
        break;
    }
}
David Kanarek