核心数据:在实体中未找到密钥错误< NSSQLEntity Studies id = 3> [英] Core Data: Keypath Error Not Found in Entity <NSSQLEntity Studies id=3>

查看:88
本文介绍了核心数据:在实体中未找到密钥错误< NSSQLEntity Studies id = 3>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Could any one tell me what's the wrong with this code? It raises the following error and cause application to crash:

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];

    }


推荐答案

由于您的抓取实体 Studies ,您不会将其包含在谓词中,因为 Studies 对象是接收谓词测试在第一位。所以你的谓词至少应该是:

Firstly, since your fetch entity is Studies you don't include it in the predicate because the Studies objects are the ones receiving the predicate test in the first place. So your predicate should be at least just:

patients.PatientName == %@

但是,按照惯例, patients 会指示一对多关系。如果是,那意味着 patients 的实际值是一组(大概) Patient 对象。因此,你不能要求一个集合的属性值如上所示:相反,你必须要求一组新的集合中的所有对象匹配的谓词。使用ANY或所有运算符,如下所示:

However, by convention, patients would indicate a to-many relationship. If so, that means that the actual value of patients is a set of (presumably) Patient objects. As such you can't ask a set for an attribute value as above: Instead you have to ask for a new set of all object in the set that match the predicate. Use the ANY or All operator like so:

ALL patients.PatientName == %@

根据约定,所有属性和关系名称都以小写字母开头,如果 PatientName 是一个属性,它应该是 patientName

I would add that by convention all attribute and relationship names start with lower case letters so if PatientName is an attribute it should be patientName.

这篇关于核心数据:在实体中未找到密钥错误&lt; NSSQLEntity Studies id = 3&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆