2

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 Answers 2

4

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];
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.