App crash when I touch specific cell in UITableView

305 Views Asked by At

I have Maintableview(a UIViewController) with contents and detailview(a UIViewController) that show detail for each of cells in Maintableview.
I have added a "favorite" button in "detailview" that user can add to Favoritetableview(a UIViewController) and every thing work fine.

Now I have added "add to favorite" in Maintableview with swipe to left. It adds contents successfully to Favoritetableview but when I touch that cell in Favoritetableview the app crashes.

Below is the console logs for the crash:

 Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010a871d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a2e3deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010a75a934 -[__NSArrayI objectAtIndex:] + 164
    3   iranbirds                           0x00000001075cf672 -[FavoriteTableViewController prepareForSegue:sender:] + 530
    4   UIKit                               0x00000001090c55d5 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 369
    5   UIKit                               0x00000001090c5433 -[UIStoryboardSegueTemplate _perform:] + 82
    6   UIKit                               0x0000000108b1b5f8 -[UIViewController performSegueWithIdentifier:sender:] + 99
    7   iranbirds                           0x00000001075cf42d -[FavoriteTableViewController tableView:didSelectRowAtIndexPath:] + 189
    8   UIKit                               0x0000000108ac51c6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1887
    9   UIKit                               0x0000000108ac541b -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 388
    10  UIKit                               0x0000000108989f62 _runAfterCACommitDeferredBlocks + 317
    11  UIKit                               0x000000010899de4c _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
    12  UIKit                               0x00000001089aa147 _afterCACommitHandler + 90
    13  CoreFoundation                      0x000000010a796c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    14  CoreFoundation                      0x000000010a796ba7 __CFRunLoopDoObservers + 391
    15  CoreFoundation                      0x000000010a78c7fb __CFRunLoopRun + 1147
    16  CoreFoundation                      0x000000010a78c0f8 CFRunLoopRunSpecific + 488
    17  GraphicsServices                    0x000000010be6bad2 GSEventRunModal + 161
    18  UIKit                               0x000000010897df09 UIApplicationMain + 171
    19  iranbirds                           0x00000001075d32ef main + 111
    20  libdyld.dylib                       0x000000010ba6492d start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

favorite table view:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath = (NSIndexPath *)sender;
    Favorite *fav = (Favorite *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *combinedName = fav.name;

    if  ([segue.identifier isEqualToString:@"FavoriteBirdDetail"])
    {
        GeneralViewController *detailViewController = (GeneralViewController*)[segue destinationViewController];

        detailViewController.birdName = [combinedName componentsSeparatedByString:@"^"][0];;
        detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];;
        detailViewController.managedOjbectContext = self.managedOjbectContext;
    }

}

This line cause error:
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];

Any help would be appreciated.

1

There are 1 best solutions below

2
On BEST ANSWER
detailViewController.sciName = [combinedName componentsSeparatedByString:@"^"][1];

Well, look at this code - you are getting second object from an array by subscript syntax. And the code would work if an array contains at least 2 items.

It looks like your array contains zero or one element, that is why app crashes when you try to get an element by 1 index - [1], which means the second element.

To prevent crash you should check an array count property before:

NSArray *anArray = [combinedName componentsSeparatedByString:@"^"]; 

if anArray.count >= 2 {
    detailViewController.sciName = anArray[1];
}