'+ entityForName:nil不是搜索实体名称的合法NSManagedObjectContext参数

时间:2013-09-01 01:31:05

标签: ios core-data ios6

我试图在表格中保存记录。 我在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;
}

2 个答案:

答案 0 :(得分:4)

[self managedObjectContext];

返回nil,我可以通过您发布的代码告诉您更多内容。

答案 1 :(得分:2)

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