【发布时间】:2013-11-11 03:10:16
【问题描述】:
编程新手**
尝试访问可变数组中的对象时出现“超出范围”的 NSRangeException。错误显示 objectAtIndex 的数字很长,但该数组当前只有三个对象。
这是错误消息:由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'* -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 2]'
我正在使用 Core Data。
当我选择通过 Core Data 填充的 tableview 的第一行时,应用程序崩溃了。
可变数组称为“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
如果这是一个愚蠢的问题,请告诉我。任何类型的资源都值得赞赏。
感谢您的帮助
【问题讨论】:
-
谢谢@WilliamFalcon,试过了,但仍然抛出同样的错误。想法?
-
看起来您正在向数组询问 indexpath 的索引。我不认为那是一组索引路径......你应该只做 allDates[selectedRowIndexPath.row]。还可以尝试将该行分解为多个部分,以便更好地调试。
-
@WilliamFalcon,做到了。如果您想将其添加为答案,我很乐意接受。非常感谢。
标签: ios arrays core-data nsrangeexception