尝试从核心数据中获取记录时索引2超出边界异常

时间:2016-04-25 06:41:51

标签: ios core-data nsrangeexception

我在segmentedControl下面有一个segmentedControl按钮和一个表视图。

在我的viewDidLoad方法中,我从核心数据中获取值并按如下方式填充数组,

 NSError *Error = nil;
    APIRequest *apiRequest = [[APIRequest alloc]init];
    [apiRequest showPendingData];    
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"PendingShipmentDetails"];
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"PendingShipmentDetails" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&Error];
    shipmentReferenceNumberArray = [[NSMutableArray alloc]init];

    for (NSManagedObjectContext * info in fetchedObjects)

    {        
        [shipmentReferenceNumberArray addObject:[info valueForKey:@"shipmentno"]];

    }

在handleSelection方法中,Im处理按钮点击如下

- (void)handleSelection:(id)sender
{
    UISegmentedControl *segmentControl = (UISegmentedControl *) sender;
    NSInteger selectedSegment = segmentControl.selectedSegmentIndex;

    if (selectedSegment == 0)
    {
        //toggle the correct view to be visible
        [self.myOrdersTableView setHidden:NO];
        [self.completedOrdersTableView setHidden:YES];
        NSLog(@"pending");
    }
    else
    {
        //toggle the correct view to be visible
        [self.completedOrdersTableView setHidden:NO];
        [self.myOrdersTableView setHidden:YES];
        [self.devices removeAllObjects];
        NSError *Error = nil;
        APIRequest *apiRequest = [[APIRequest alloc]init];
        [apiRequest showCompletedData];

        NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"CompletedShipmentDetails"];
        self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"CompletedShipmentDetails" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];
        NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&Error];


    }
}

现在,如果按下索引为1的分段控制按钮,则会出现以下错误

  
      
  • 由于未捕获的异常终止应用' NSRangeException',原因:' * - [__ NSArrayM objectAtIndex:]:索引2超出边界[0 ..   1]'   * 第一次抛出调用堆栈:(0x2867d5f7 0x36347c77 0x28591157 0x15bbd3 0x2bde795d 0x2bde7a1f 0x2bddd141 0x2bbf51cb 0x2bb1f19f 0x2b54af65   0x2b546951 0x2b5467d9 0x2b5461c7 0x2b545fd1 0x2b5989f1 0x2fe5f82f   0x295a71ed 0x286333a5 0x286437f3 0x2864378f 0x28641db1 0x2858eb31   0x2858e943 0x2f952051 0x2bb80f21 0x164195 0x368e3aaf)libc ++ abi.dylib:   以NSException(lldb)
  • 类型的未捕获异常终止   

我该如何解决这个问题?

0 个答案:

没有答案