0

titleFirstLetterBook エンティティに一時属性があります。

- (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>'

助けてください!

4

1 に答える 1

0

セクション インデックスだけが必要で、セクション ヘッダーが不要な場合は、title 属性を渡すだけで、探しているものを実現できます。

sectionNameKeyPath:@"title"

それ以外の場合、最初の文字だけを含むセクション ヘッダーが実際に必要な場合は、この前の質問で詳細な説明を参照してください。

于 2011-11-20T01:07:16.487 に答える