0

我正在关注将现有数据库预加载到核心数据中的教程。代码编译得很好,直到我到达这一行:

NSEntityDescription *entity = [NSEntityDescription    insertNewObjectForEntityForName:@"FailedBankInfo" inManagedObjectContext:context];

它返回以下错误:2012-11-19 12:45:02.247 CoreDatTutorial2[2049:403] -[FailedBankInfo subentitiesByName]: unrecognized selector sent to instance 0x100301010'

我已经检查以确保它不为空并且它已保存到产品目录中。知道我做错了什么吗?如果我注释掉那行代码,项目编译没有问题。

这是其余的代码:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankInfo" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (FailedBankInfo *info in fetchedObjects){
    {
4

1 回答 1

2
insertNewObjectForEntityForName:inManagedObjectContext:

创建一个新的托管对象并将其插入到托管对象上下文中。特别是,它返回类的对象FailedBankInfo(在您的情况下),而不是NSEntityDescription.

要创建NSEntityDescription,请使用entityForName:inManagedObjectContext:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo"
                                inManagedObjectContext:context];
[fetchRequest setEntity:entity];

请注意(从 iOS 5.0 开始)您可以将其简化为

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"FailedBankInfo"];
于 2012-11-19T20:39:53.860 回答