1

on the first screen user enters data & displaying it in second ViewController using prepareforsegue method.on the second screen user selects multiple rows for delete then again user selects remaining rows for delete application crash.Here is my code

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray removeObjectsAtIndexes:]: index 5 in index set beyond bounds [0 .. 4]'

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return mAryValue.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * strIdent=@"id1Cell"; UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:strIdent]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdent]; } cell.textLabel.text=[mAryValue objectAtIndex:indexPath.row]; return cell; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (self.editing) { return; } [_tblView deselectRowAtIndexPath:[_tblView indexPathForSelectedRow] animated:NO]; UITableViewCell *cell = [_tblView cellForRowAtIndexPath:indexPath]; if (cell.accessoryType == UITableViewCellAccessoryNone) { cell.accessoryType = UITableViewCellAccessoryCheckmark; [mArySel addObject:indexPath]; } else if(cell.accessoryType == UITableViewCellAccessoryCheckmark) { cell.accessoryType = UITableViewCellAccessoryNone; [mArySel removeObjectIdenticalTo:indexPath]; } } -(void)getData:(NSMutableArray *)userValues { mAryValue=userValues; } - (IBAction)btnDelete:(id)sender { if (mAryValue==nil || mAryValue.count==0) { UIAlertController * alert= [UIAlertController alertControllerWithTitle:@"Alert" message:@"Please Enter Value" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; [alert addAction:ok]; [alert addAction:cancel]; [self presentViewController:alert animated:YES completion:nil]; } else { NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init]; for (NSIndexPath *indexPath in mArySel) { [indicesToDelete addIndex:indexPath.row]; } if (!(indicesToDelete==nil) || !(indicesToDelete.count==0)) { [mAryValue removeObjectsAtIndexes:indicesToDelete]; } [_tblView reloadData]; } } 

Please suggest me. Thank you.

5
  • show the crash report Commented Feb 17, 2017 at 9:48
  • *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray removeObjectsAtIndexes:]: index 5 in index set beyond bounds [0 .. 4]' Commented Feb 17, 2017 at 9:54
  • check once this line in your code [mAryValue removeObjectsAtIndexes:indicesToDelete]; , the tableview data source try to access the 5/4 index. Commented Feb 17, 2017 at 10:01
  • Thank you for help.its working.. Commented Feb 17, 2017 at 10:04
  • welcome have a happy day Commented Feb 17, 2017 at 10:09

1 Answer 1

1

[NSMutableArray removeObjectsAtIndexes:]: index 5 in index set beyond bounds [0 .. 4]'

You are trying to remove an object at an index on your array that does not exist. As the error says, you are removing objectAtIndex:5, but your array only has 4 items.

[mAryValue removeObjectsAtIndexes:indicesToDelete]; 
Sign up to request clarification or add additional context in comments.

1 Comment

@AhmedSahib He should get the correct index to start with, not run magic numbers as you suggest.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.