【问题标题】:Transient attributes and FetchRequest?瞬态属性和 FetchRequest?
【发布时间】:2023-03-29 10:38:01
【问题描述】:

我在 Book Entity 中有一个临时属性 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>'

请帮忙!

【问题讨论】:

标签: ios entity transient


【解决方案1】:

如果你只想要section index,而不想要section headers,你可以只传递title属性,它会实现你想要的。

sectionNameKeyPath:@"title"

否则,如果您确实想要只有第一个字母的部分标题,请参阅this previous question 进行完整讨论。

【讨论】:

    猜你喜欢
    • 2011-11-23
    • 1970-01-01
    • 2011-02-10
    • 2011-03-13
    • 2011-06-17
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多