ios - 数组 objectAtIndex <huge number> 上的 RangeException #Core Data

标签 ios arrays core-data nsrangeexception

编程新手**

尝试从可变数组访问对象时出现“超出范围”NSRangeException。该错误显示 objectAtIndex 的数字很长,但该数组当前只有三个对象。

这是错误消息:由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayM objectAtIndex:]:索引 2147483647 超出范围 [0 .. 2]”

我正在使用核心数据。

当我选择通过核心数据填充的表格 View 的第一行时,应用程序崩溃。

可变数组称为“allDates”。

似乎导致它的代码位于此处的prepareForSegue方法中:

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

如果这是一个愚蠢的问题,请告诉我。任何类型的资源都受到赞赏。

感谢您的帮助

最佳答案

indexOfObject: 返回 2147483647 的事实并非侥幸。 2147483647 相当于 NSNotFound 相当于 NSIntegerMax,这是当您指定的对象不存在于数组中的任何索引处,或者换句话说,“未找到”。

来自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.

这有助于我们了解您的应用崩溃的原因。显然,您无法访问数组中任何大于数组计数减一的索引处的元素,并且 2147483647 是一个比数组计数大得多的数字,这会导致范围异常被抛出。幸运的是,这个问题很容易解决,您所要做的就是在索引大于或等于数组的计数时防止对数组的访问。这是一个例子:

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!";
}

关于ios - 数组 objectAtIndex <huge number> 上的 RangeException #Core Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19898229/

相关文章:

javascript - 在 javascript 中打印图像数组时出现问题

javascript - 如何按顺序对数组中的重复值求和

javascript - 创建一个具有随机值的数组

ios - 使用 NSFetchedResultsController 理解 transient 属性

iPhone - 核心数据 NSInteger、integer32、int 或其他

ios - 获取核心数据时出现错误

ios - 防止或检测从 iOS 8 键盘传递的事件

ios - 台风不注入(inject)属性(property)(无 Storyboard)

ios - AFNetworking,使用正确的 token 获取 401

ios - 方向改变时居中多个标签?