瞬态属性和 FetchRequest?

发布于 2024-12-12 07:27:43 字数 1668 浏览 0 评论 0原文

我在图书实体中有一个瞬态属性 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>'

请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2024-12-19 07:27:43

如果您只想要节索引,而不想要节标题,您可以只传递 title 属性,它将实现您正在寻找的内容。

sectionNameKeyPath:@"title"

否则,如果您实际上想要仅包含第一个字母的节标题,请参阅 上一个问题进行全面讨论。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文