objective-c - 核心数据 : Keypath Error Not Found in Entity <NSSQLEntity Studies id=3>

标签 objective-c core-data nspredicate nsfetchrequest

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

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 对象是首先接受谓词测试的对象。所以你的谓词至少应该是:

patients.PatientName == %@

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

ALL patients.PatientName == %@

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

关于objective-c - 核心数据 : Keypath Error Not Found in Entity <NSSQLEntity Studies id=3>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5182969/

相关文章:

ios - 如何在 CoreData 的 NSLog 中显示详细信息?建议我一个解决方案

iphone - 如何备份使用 Core Data - iPhone 应用程序存储的数据?

cocoa - 从 XML 切换到 SQLite 核心数据存储时存在哪些潜在障碍?

iphone - 如何在 iPad 中显示 UIDatePicker?

objective-c - 报亭应用订阅(应用内购买)困惑

ios - 当部分 map 被覆盖时居中 MKMapView

iphone - 以编程方式访问应用标识符前缀

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

ios - NSPredicate 在 Objective C 中查找多个单词

ios - NSPredicate,获取结果与一对多关系的子集