1

我正在尝试将记录保存在表格中。我阅读了 StackOverflow 中的所有问题和答案,但无法解决问题。当我尝试保存数据时,出现错误:

2013-08-31 22:25:37.321 OnTheRoadV2[26581:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Trip''
*** First throw call stack:
(0x18e7012 0x1427e7e 0x743e7 0xba402 0x41db 0x143b705 0x372920 0x3728b8 0x433671 0x433bcf 0x432d38 0x3a233f 0x3a2552 0x3803aa 0x371cf8 0x2404df9 0x2404ad0 0x185cbf5 0x185c962 0x188dbb6 0x188cf44 0x188ce1b 0x24037e3 0x2403668 0x36f65c 0x29ed 0x2915)
libc++abi.dylib: terminate called throwing an exception

我的代码:

- (IBAction)start:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    NSManagedObject *newTrip = [NSEntityDescription insertNewObjectForEntityForName:@"Trip" inManagedObjectContext:context];
    [newTrip setValue:self.name.text forKey:@"name"];

    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
        _status.text = @"Trip started!";


}

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}
4

2 回答 2

4
[self managedObjectContext];

返回零,我无法从您发布的代码中告诉您更多信息。

于 2013-09-01T01:34:59.863 回答
2

如果您不熟悉核心数据,请使用神奇的记录。它易于集成:这里是教程: http ://ablfx.com/blog/article/2

于 2013-09-01T03:34:59.147 回答