ios – 自定义核心数据SectionNameKeyPath

前端之家收集整理的这篇文章主要介绍了ios – 自定义核心数据SectionNameKeyPath前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是新的核心数据,我试图找出如何在我的NSFetchedResultsController中创建一个自定义的sectionNameKeyPath.我有一个名为acctPeriod的属性的托管对象.这是一个NSString.我想根据该字段的前4个字符创建节.前4个字符代表会计期间的年份,不需要保存.

我已经通过这个网站,看过关于瞬态属性的帖子,但我似乎不能让他们上班.基本上我想要这个,然后为我的sectionNameKeyPath分配periodYear.

@dynamic periodYear;

-(NSString *)periodYear
{
    return [self.acctPeriod substringToIndex:4];
}

任何帮助将不胜感激.

**更新:
使用马丁R.答案,我能够让它按预期工作.

- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Billing" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"acctPeriod" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];

//Predicate
NSPredicate *pred = [NSPredicate predicateWithFormat:@"clients = %@",self.client];
NSLog(@"%@",pred);

//[fetchRequest setResultType:NSDictionaryResultType];
//[fetchRequest setReturnsDistinctResults:YES];

[fetchRequest setPredicate:pred];
[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"periodYear" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
    NSLog(@"Unresolved error %@,%@",error,[error userInfo]);
    abort();
}

return _fetchedResultsController;  
}

解决方法

以下内容应该运行:实现PeriodYear方法(将使用它)
作为“分区名称键路径”)在您的托管对象子类的类扩展中:
@interface Event (AdditionalMethods)
- (NSString *)periodYear;
@end

@implementation Event (AdditionalMethods)
- (NSString *)periodYear {
    return [self.acctPeriod substringToIndex:4];
}
@end

确保acctPeriod被用作提取请求的第一个(或唯一)排序描述符:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"acctPeriod" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];

对于获取的结果控制器,使用periodYear作为sectionNameKeyPath:

NSFetchedResultsController *_fetchedResultsController = [[NSFetchedResultsController alloc]
                  initWithFetchRequest:fetchRequest 
                   managedObjectContext:self.managedObjectContext 
                     sectionNameKeyPath:@"periodYear"
                              cacheName:nil];
_fetchedResultsController.delegate = self;
self.fetchedResultsController = _fetchedResultsController;

最后添加默认的titleForHeaderInSection方法

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}

或者,您可以将periodYear定义为受管对象的临时属性.
在这种情况下,它也不会存储在数据库中,但可以以按需和缓存计算值的方式实现.

Apple开发人员库中的DateSectionTitles示例项目演示了如何工作.

原文链接:https://www.f2er.com/iOS/337270.html

猜你在找的iOS相关文章