16

谁能告诉我这段代码有什么问题?它引发以下错误并导致应用程序崩溃:

reason: 'keypath Studies.patients.PatientName not found in entity <NSSQLEntity Studies id=3>'

代码:

 - (void)viewDidLoad {
        [super viewDidLoad];

        test_coredataAppDelegate *appDelegate = (test_coredataAppDelegate *)[[UIApplication sharedApplication] delegate];
        self.context = appDelegate.managedObjectContext;


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription 
                                       entityForName:@"Studies" inManagedObjectContext:_context];
        [fetchRequest setEntity:entity];
        /**/
        NSLog(patientName);
        [fetchRequest setPredicate:[NSPredicate predicateWithFormat:
                               @"(Studies.patients.PatientName == %@ )",patientName]];



        NSError *error;
        self.StudiessList = [_context executeFetchRequest:fetchRequest error:&error];
        self.title = @"patients"; 
        [fetchRequest release];

    }
4

4 回答 4

18

首先,由于您的 fetch 实体是Studies您不将其包含在谓词中,因为Studies对象是首先接收谓词测试的对象。所以你的谓词至少应该是:

patients.PatientName == %@

但是,按照惯例,patients这将表示一对多关系。如果是这样,这意味着 的实际值patients是一组(大概)Patient对象。因此,您不能向集合请求上述属性值:相反,您必须请求集合中与谓词匹配的所有对象的新集合。像这样使用 ANY 或 All 运算符:

ALL patients.PatientName == %@

我要补充一点,按照惯例,所有属性和关系名称都以小写字母开头,所以如果PatientName是属性,它应该是patientName

于 2011-03-03T20:59:11.930 回答
6

Studies 实体没有患者属性,或者患者关系指向的任何实体都没有 PatientName 属性(注意大写/小写问题)或两者兼而有之。

于 2011-03-03T15:50:46.027 回答
1

通常当我看到这个错误时,这意味着我向我的模型添加了一个新版本,但忘记更新项目的当前模型版本。

Xcode 属性截图

发布此答案以帮助我未来的自己,因为这不是我第一次登陆此页面!

于 2019-11-28T11:48:07.563 回答
0

在我的例子中,我引用了实体的超类属性,但没有在xcdatamodel文件中设置父实体。我必须NSManagedObject从检查器菜单中选择另一个子类(默认为无):

xcdatamodel 片段

于 2021-01-16T23:42:55.987 回答