【问题标题】:'NSInvalidArgumentException', reason: 'keypath Patient.nachname not found in entity <NSSQLEntity Patient id=1>''NSInvalidArgumentException',原因:'keypath Patient.nachname not found in entity <NSSQLEntity Patient id=1>'
【发布时间】:2014-01-27 22:17:32
【问题描述】:

为什么我会收到 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;

}

我的病人.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

我的病人.m:

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

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

为什么我会遇到异常?

* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“keypath Patient.nachname not found in entity”

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

【问题讨论】:

    标签: ios core-data nsfetchedresultscontroller


    【解决方案1】:

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

    即它应该是

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

    下一个排序描述符和“vorname”也是如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2015-04-10
      • 2017-05-16
      • 2022-06-27
      • 1970-01-01
      • 2015-10-19
      相关资源
      最近更新 更多