0

I have a transient attribute titleFirstLetter in the Book Entity:

- (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;
}

and I am trying to use this attribute as Section name but when I execute:

 // 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;

I get this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',  
reason: 'keypath titleFirstLetter not found in entity <NSSQLEntity Book id=1>'

please help!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Marta V.
  • 11
  • 1

1 Answers1

0

If you just want the section index, and don't want section headers, you can just pass the title attribute and it will achieve what you are looking for.

sectionNameKeyPath:@"title"

Otherwise, if you actually want section headers that have just the first letter, see this previous question for a full discussion.

Community
  • 1
  • 1
Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93