2011-10-27 4 views
0

私はブックエンティティで一時属性titleFirstLetterあります一時属性とFetchRequest?

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

http://stackoverflow.com/a/3553471 – malaba

答えて

0

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

sectionNameKeyPath:@"title" 

そうでない場合は、あなたが実際には最初の文字を持っているセクションヘッダをしたい場合は、十分な議論のためのthis previous questionを参照してください。

関連する問題