objective-c : Use a array to point to another array

37 views Asked by At

I have two Mutable arrays. "friendsArray" contains a whole list os users and "inviteIndex" contains the indexes (indexPath.row) of some of them (get using uiswitch button into a table list).

The I fill inviteIndex array as:

[inviteIndex addObject:[NSNumber numberWithInteger:indexPath.row]];

In order to have only selected users I do:

  for (int i =0; i< [inviteIndex count]; i++) {

        MyElement  *friend =[friendsArray objectAtIndex:[inviteIndex objectAtIndex:i] ];

        NSLog(@"BUTTON PRESSED %@", friend.friendId);

    }

but app crash with message:

[__NSArrayM objectAtIndex:]: index 400931632 beyond bounds [0 .. 4]'

I trid used
MyElement *friend =[friendsArray objectAtIndex:(NSInteger)[inviteIndex objectAtIndex:i] ]; with same result.

Any help please? Thanks in advance.

2

There are 2 answers

0
trojanfoe On BEST ANSWER

[inviteIndex objectAtIndex:i] will return an NSNumber object. You want the NSInteger value within the NSNumber object:

for (int i =0; i< [inviteIndex count]; i++) {

    NSInteger index = [[inviteIndex objectAtIndex:i] integerValue];
    MyElement  *friend =[friendsArray objectAtIndex:index];

    NSLog(@"BUTTON PRESSED %@", friend.friendId);

}
0
Hemang On

index beyond bounds this error comes when you're trying to access an object from an index which are not exist in array.

In your case, seems that friendArrays count is < (less than the) inviteIndex count.

I think the problem is somewhere in this logic, [inviteIndex addObject:[NSNumber numberWithInteger:indexPath.row]];

Check count of inviteIndex.