Ios 由于未捕获异常而终止应用程序';nsrange异常';,原因:';***-[\uu NSArrayI objectAtIndex:]:索引3超出范围[0..2]
当我运行iOS时,显示主菜单,以及主菜单类别和产品的两部分。单击类别时,会发生此错误:Ios 由于未捕获异常而终止应用程序';nsrange异常';,原因:';***-[\uu NSArrayI objectAtIndex:]:索引3超出范围[0..2],ios,objective-c,nsarray,nsrangeexception,Ios,Objective C,Nsarray,Nsrangeexception,当我运行iOS时,显示主菜单,以及主菜单类别和产品的两部分。单击类别时,会发生此错误: Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2] 代码是: #import "IMScategoriesViewController.h" #import "IMSAddCategory
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]
代码是:
#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;
}
Log
categories数组。我看到numberofrowsinssection
是根据数组的计数4定义的,但是类别可能有一个nil
对象
因此,类别的计数为4,但在尝试访问它时会崩溃
NSManagedObject *category = [self.categories objectAtIndex:indexPath.row];
这显然意味着您正试图访问某个数组的索引,超出其限制。找出此异常涉及的数组的最佳方法是为所有异常设置一个断点并尝试调试。您试图访问数组的第四个元素
类别
,如果将核心数据用于表,然后,您应该查看NSFetchedResultsController,而不是自己管理它。在self.categories=[[managedObjectContext executeFetchRequest:fetchRequest错误:nil]mutableCopy]之后添加NSLog(@“%@”,self.categories”);
;