Charlie C
Charlie C

Reputation: 21

Extracting the index of an Xcode array

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?

Upvotes: 2

Views: 9080

Answers (2)

David Kanarek
David Kanarek

Reputation: 12613

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

Upvotes: 0

Robert Höglund
Robert Höglund

Reputation: 10070

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

Upvotes: 4

Related Questions