'NSInvalidArgumentException',原因:'keypath在实体中找不到Patient.nachname <nssqlentity patient =“”id =“1”>'</nssqlentity>

时间:2014-01-27 22:17:33

标签: ios core-data nsfetchedresultscontroller

为什么我收到NSInvalidArgumentException?

我的表视图控制器代码:

- (NSFetchedResultsController *)fetchedResultsController {

CoreDataHelper *cdh = [(MRMedSafeAppDelegate *) [[UIApplication sharedApplication] delegate] cdh];

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

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

NSSortDescriptor  *sort1 = [[NSSortDescriptor  alloc]
                            initWithKey:@"Patient.nachname" ascending:YES];
NSSortDescriptor  *sort2 = [[NSSortDescriptor  alloc]
                            initWithKey:@"Patient.vorname" ascending:YES];

[fetchRequest setSortDescriptors:[NSArray  arrayWithObjects:sort1, sort2, nil]];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:cdh.context
                                      sectionNameKeyPath:nil
                                               cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

return _fetchedResultsController;

}

My Patient.h:

@interface Patient : NSManagedObject
@property (nonatomic, retain) NSDate * geburtsdatum;
@property (nonatomic, retain) NSString * nachname;
@property (nonatomic, retain) NSString * vorname;
@property (nonatomic, retain) NSNumber * weiblich;
@end

我的病人。:

#import "Patient.h"
@implementation Patient
@dynamic geburtsdatum;
@dynamic nachname;
@dynamic vorname;
@dynamic weiblich;
@end

由Model.xcdatamodeld文件中的XCode 5生成的Patient.h和Patient.h。

为什么我会收到例外?

  

* 由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'keypath在实体中找不到Patient.nachname'

患者是唯一的类,模型中没有其他模型类,也没有关系。

1 个答案:

答案 0 :(得分:1)

中使用“nachname”代替“Patient.nachname”
NSSortDescriptor  *sort1 = [[NSSortDescriptor  alloc]
                        initWithKey:@"Patient.nachname" ascending:YES];

即。它应该读

NSSortDescriptor  *sort1 = [[NSSortDescriptor  alloc]
                        initWithKey:@"nachname" ascending:YES];

同样适用于下一个排序描述符和“vorname”。