我在图书实体中有一个瞬态属性titleFirstLetter
:
- (NSString *)titleFirstLetter
{
NSString *tmpValue = nil;
[self willAccessValueForKey:@"titleFirstLetter"];
if ([[self title] length] > 0) {
tmpValue = [[[self title] substringToIndex:1] uppercaseString];
if ([[NSScanner scannerWithString:tmpValue] scanInt:NULL]) { //return # if its a number
tmpValue = @"#";
}
} else { //sanity in case the attribute is not set.
tmpValue = @"";
}
[self didAccessValueForKey:@"titleFirstLetter"];
return tmpValue;
}
我试图将此属性用作节名,但是当我执行时:
// Create the sort descriptors array.
NSSortDescriptor *authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"titleFirstLetter" ascending:YES];
NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, titleDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"titleFirstLetter" cacheName:nil];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;
我收到此错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'keypath titleFirstLetter not found in entity <NSSQLEntity Book id=1>'
请帮忙!
答案 0 :(得分:0)
如果你只想要部分索引,并且不想要部分标题,你可以只传递title属性,它将实现你想要的。
sectionNameKeyPath:@"title"
否则,如果您确实希望部分标题只包含第一个字母,请参阅this previous question进行完整讨论。