Keypath <transientproperty>在实体中找不到

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

我使用下面的代码。但它抛出一个exception*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath dateSectionIdentifier not found in entity <NSSQLEntity Expense id=1>'

猜测添加sorting描述符时会发生exception。

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

//Expense.h

 NSString *dateSectionIdentifier; 

//Expense.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; } 

你的问题的标题表明“dateSectionIdentifier”是一个瞬态属性。

如果SQLite用作存储types,则不能在核心数据获取请求的sorting描述符(或谓词)中使用transient属性。 这是一个logging的限制,只能使用持久属性。

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

我想你会在子类中添加“dateSectionIdentifier”,但没有在.xcdatamodelId文件中更新。 交叉检查是否在.xcdatamodelId文件中添加了“dateSectionIdentifier”。