2
votes

Novice to Programming**

Getting a "beyond bounds" NSRangeException when trying to access an object from a mutable array. The error is showing a long number for the objectAtIndex, but the array only currently has three objects.

This is the error message: Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 2]'

I am using Core Data.

The app is crashing when I select the first row of a tableview that is populated via Core Data.

The mutable array is called "allDates".

The code that seems to be causing it is in a prepareForSegue method here:

Part of DateTableViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

NSString *segueIdentifier = [segue identifier];
    if ([segueIdentifier isEqualToString:@"showDetails"])
    {
        NSManagedObjectContext *context = [self managedObjectContext];

        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Date"
                                                  inManagedObjectContext:context];
        [fetchRequest setEntity:entity];


        self.allDates = [[context executeFetchRequest:fetchRequest error:nil] mutableCopy];
        DateIdeaDetailViewController *vc = [segue destinationViewController];

        NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];

         NSUInteger index = [allDates indexOfObject:selectedRowIndexPath];
        //edited based on WilliamFalcon added "(int)"
        _string = [NSMutableString stringWithFormat:@"%@", [allDates[(int)index] valueForKey:@"date_idea"]];

       vc.dateIdeaLabelText = self.string;

    }

}

DateTableViewController.h

#import <UIKit/UIKit.h>
#import "LifeBook.h"
#import "LifeBookAppDelegate.h"
#import "DateIdeaDetailViewController.h"

@interface DateTableViewController : UITableViewController 

@property (strong, nonatomic) NSMutableArray *allDates;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (weak, nonatomic) IBOutlet UITableViewCell *tableViewCell1;
@property (strong, nonatomic) NSMutableString *string;

@property (strong, nonatomic) NSString *label;


@end

Please let me know if this is a stupid question. Resources of any type are appreciated.

Thanks for any help

2
Thanks @WilliamFalcon, tried, but still throwing the same exact error. Thoughts?Clark
It looks like you are asking your array for the index of the indexpath. I dont think thats an array of indexpaths... You should just do allDates[selectedRowIndexPath.row]. Also try to break up that line into parts so you can debug better.William Falcon
@WilliamFalcon, that did it. If you want to add it as an answer I would gladly accept it. Thank you so much.Clark

2 Answers

2
votes

The fact that indexOfObject: is returning 2147483647 is no fluke. 2147483647 is equivalent to NSNotFound is equivalent to NSIntegerMax, which is the value that is returned by indexOfObject: when the object you specified doesn't exist at any index in the array, or is in other words, "not found".

From the docs

The lowest index whose corresponding array value is equal to anObject. If none of the objects in the array is equal to anObject, returns NSNotFound.

Which works us up to why your app is crashing. You obviously can't access an element in an array at any index greater than one less than the array's count, and 2147483647 is a much larger number than the count of your array, which results in the range exception being thrown. Fortunately, this problem is easy enough to solve, all you have to do is guard the access of the array from being possible when the index is greater than or equal to array's count. Here's an example:

if (index < allDates.count) {
    _string = [NSMutableString stringWithFormat:@"%@", [allDates[(int)index] valueForKey:@"date_idea"]];
   vc.dateIdeaLabelText = self.string;
}else{
    // do something else..
    vs.dateIdeaLabelText = @"Something went wrong!";
}
1
votes

It looks like you are asking your array for the index of the indexpath. I dont think that's an array of indexpaths...

You should just do allDates[selectedRowIndexPath.row].

Also try to break up that line into parts so you can debug better.