Search code examples
iosnsmutablearraynsindexpath

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


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.


Solution

  • [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);
    
    }