13

我想在表格视图的部分标题中显示格式化的日期..

我使用了以下代码。但它引发了异常*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath dateSectionIdentifier not found in entity <NSSQLEntity Expense id=1>'

猜测添加排序描述符时会出现异常。

NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithCapacity:20];
NSSortDescriptor *mainSortDescriptor = [[NSSortDescriptor alloc] initWithKey:dateSectionIdentifier ascending:NO];
[sortDescriptors addObject:mainSortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];

//费用.h

NSString *dateSectionIdentifier;

//费用.m

@dynamic dateSectionIdentifier

-(NSString *)dateSectionIdentifier{
[self willAccessValueForKey:@"dateSectionIdentifier"];
NSString *tempDate = [self primitiveDateSectionIdentifier];
[self didAccessValueForKey:@"dateSectionIdentifier"];
if(!tempDate){
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"d MMMM yyyy"];
    tempDate = [dateFormatter stringFromDate:[self date]];
    [self setPrimitiveDateSectionIdentifier:tempDate];
    [dateFormatter release];
}
return tempDate;

}
4

2 回答 2

31

您问题的标题表明“dateSectionIdentifier”是一个临时属性。

如果 SQLite 用作存储类型,则不能在 Core Data 获取请求的排序描述符(或谓词)中使用瞬态属性。这是一个记录的限制,只能使用持久属性。

有关更多信息,请参阅“核心数据编程指南”中的持久存储类型和行为

于 2013-04-12T13:30:11.467 回答
0

我认为您会在子类中添加“dateSectionIdentifier”,但不会在 .xcdatamodelId 文件中更新。交叉检查以查看您是否在 .xcdatamodelId 文件中添加了“dateSectionIdentifier”。

于 2015-09-04T11:35:57.240 回答