iphone - 核心数据不断出现 sigabrt 错误

标签 iphone cocoa-touch cocoa core-data

我目前正在学习核心数据,并正在研究苹果核心数据教程(位置)的简单版本。

下面是 addEvent 方法的代码:

Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext];
[event setCreationDate:[NSDate date]];


NSError *error;
if (![managedObjectContext save:&error]) {
    // Handle the error.
}


[eventsArray insertObject:event atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

触发此方法时,我收到 sigabrt 错误。但是我发现,如果我包含苹果的 viewDidLoad 代码,错误就不会再发生,而且我一生都无法弄清楚为什么?

这是阻止错误发生的代码:

- (void)viewDidLoad {

[super viewDidLoad];

// Set the title.
self.title = @"Locations";

// Configure the add and edit buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;

addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addEvent)];
self.navigationItem.rightBarButtonItem = addButton;



/*
 Fetch existing events.
 Create a fetch request; find the Event entity and assign it to the request; add a sort descriptor; then execute the fetch.
 */

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

// Order the events by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error.
}

// Set self's events array to the mutable array, then clean up.
[self setEventsArray:mutableFetchResults];
[mutableFetchResults release];
[request release];

我错过了什么?我不明白 viewDidLoad 上缺少获取请求是如何导致错误的

谢谢

最佳答案

你的表格 View 如何知道要显示哪些事件?

Apple 的 viewDidLoad 方法的一个副作用是它将创建 eventsArray。如果您从未创建过该行,但您告诉 tableView 您已插入一行,则最好让该行可用!

你正在打电话

[eventsArray insertObject:event atIndex:0];

但我敢打赌,如果没有 Apple 的 viewDidLoad 方法,eventsArray 就是 nil - 如果它是 nil<,您将需要创建它 (即 eventsArray = [[NSMutableArray alloc] init];

关于iphone - 核心数据不断出现 sigabrt 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7283018/

相关文章:

iphone - 在 iPhone 中,要支持蓝牙耳机,我需要做些什么吗?

iphone - 有没有一种方法可以在没有 iPhone 的情况下在 iPhone 上测试网站?

iphone - 更改页面时阻止 iphone safari 地址栏显示

iphone - 可以使用 Core Animation 使状态栏消失/溶解吗?

cocoa - 是否可以以编程方式在 Cocoa 中打印 IKImageBrowserView?

iOS 应用程序在后台显示通知,但不在前台 iPhone 4s 和操作系统版本 9.3

ios - 代码仅在我在实现 {} 中初始化对象时才有效

objective-c - 将调度信号量与委托(delegate)方法一起使用

objective-c - dispatch_sync() 总是在主线程中执行 block

objective-c - 如何在 OSX 应用程序中拉伸(stretch)图像?