In Visual Basic, there's a line of code you can use to handle errors in code
"On Error Resume Next"
Is there something similar in Objective-C? My problem is I have an "Add New Cell" add button that creates a new cell during runtime. However, the cells are re-orderable. When you try to re-order the "Add New Cell" cell, I get an index out of bounds error. If I can just fix the error from occurring that would be great, but if there's an error handler I can use to just exit the move function when there is an error, that would be cool too.
Here is my moveRowAtIndexPath code:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
iProfileAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate moveFromOriginal:fromIndexPath.row toNew:toIndexPath.row];
}
Here is the error i receive:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (7) beyond bounds (7)'
This is an array bounds error and should be fixed.
Have you tried preventing the user from moving the "Add New Cell" cell?
To do this, implement tableView:canMoveRowAtIndexPath: and return NO for the "Add New Cell" cell row (YES for all other rows).
You can use try and catch. They are how objective c, and some others, handle exceptions. They will catch the "uncaught exception" similar to how I assume would "On Error Resume Next" works.
@try {
/*this is where the code that might throw an exception goes*/
[appDelegate moveFromOriginal:fromIndexPath.row toNew:toIndexPath.row];
}
@catch (NSException *exception) {
/*add something here if you want it to do something special when the exception(or "error") is thrown*/
}
@finally {
/*you don't have to include this finally part but if you do the code in it is excecuted weather the exception is thrown or not*/
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With