Search code examples
iosobjective-cnsarraynsrangeexception

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]


When I am running the iOS, then show the main menu, and two parts of the main menu category and product. When I click on the category this error is occurrs:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2] 

The Code is:

#import "IMScategoriesViewController.h"
#import "IMSAddCategoryViewController.h"
#import "IMSAppDelegate.h"
#import "Category.h"

@interface IMScategoriesViewController ()
@property (strong) NSMutableArray *categories;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

@end

@implementation IMScategoriesViewController
- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];

   - (void)viewDidLoad
{
    [super viewDidLoad];
    // Fetch the devices from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Category"];
    self.categories = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self.tableView reloadData];
}
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return self.categories.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        // Configure the cell...
        NSManagedObject *category = [self.categories objectAtIndex:indexPath.row];
        [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [category valueForKey:@"name"], [category valueForKey:@"itemtype"]]];
        //[cell.detailTextLabel setText:[device valueForKey:@"company"]];

      //Category *category = [array objectAtIndex:indexPath.row];

       // cell.textLabel.text =[category  name];
       cell.detailTextLabel.text = [category description];

        return cell;


    }

Solution

  • Log the categories array. I see the numberOfRowsInSection is defined based on the count of the array is 4, however the categories might well have a nil object.

    Hence the count of categories is 4 but the when trying to access it crashes.

    NSManagedObject *category = [self.categories objectAtIndex:indexPath.row];