gpt4 book ai didi

iphone - 使用核心数据按工作日分组

转载 作者:行者123 更新时间:2023-12-03 20:21:41 24 4
gpt4 key购买 nike

在我的核心数据模型中,我有一个具有日期属性的实体,正如标题所示,我想按(周)天对该实体进行分组。

问题是,日期或多或少存储为时间戳,我不知道如何创建一个能够适当分组/过滤我的实体的谓词。

我发现我可能需要每天进行一次提取,因此创建了以下方法。我需要帮助的代码就在它的中间。

- (NSFetchedResultsController *)fetchedResultsController:(NSDate *)day  {
if(fetchedResultsController != nil)
return fetchedResultsController;

// Create and Configure Request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

// Predicate
// pseudo code where i'm clueless is marked by "<" and ">" - start
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"DateAttribute BETWEEN <first second of day> AND <last second of day>"];
// or
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"<dayofmonth-month-year of DateAttribute> LIKE <dayofmonth-month-year of day>"];
[request setPredicate:predicate];
// pseudo code where i'm clueless is marked by "<" and ">" - end

// Sort descriptors
NSSortDescriptor *titleDescriptor = [[NSSortDescriptor alloc] initWithKey:sortDescriptorName ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:titleDescriptor];
[request setSortDescriptors:sortDescriptors];

// create and init fetchResultsController
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;

//Memory
[request release];
[titleDescriptor release];
[aFetchedResultsController release];

return fetchedResultsController;

}

我真的很感激任何帮助。谢谢

最佳答案

如果你想按工作日分组,最简单的方法就是向你的实体添加一个属性,例如“weekday”;然后在初始化 NSFetchedResultsController 时将“weekday”作为sectionNameKeyPath 参数传递。在这种情况下,无需指定谓词:您将自动获取工作日的部分。

如果要按天分组,请在初始化 NSFetchedResultsController 时将 DateAttribute 作为sectionNameKeyPath 参数传递。同样,无需指定谓词:您将自动获取当天的部分。

您在代码中尝试执行的操作与您要求的操作不同。事实上,您并不是试图取回部分(即按属性分组):您而是试图取回 tableView 的单个部分,其中包含满足特定谓词的实体所描述的对象,即 DateAttribute 落下的对象(可能是)在当天。为此,您可以使用以下代码:

// start by retrieving day, weekday, month and year components for today
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
NSInteger theDay = [todayComponents day];
NSInteger theMonth = [todayComponents month];
NSInteger theYear = [todayComponents year];

// now build a NSDate object for the input date using these components
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:theDay];
[components setMonth:theMonth];
[components setYear:theYear];
NSDate *thisDate = [gregorian dateFromComponents:components];
[components release];


// now build a NSDate object for tomorrow
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
[offsetComponents release];

NSDateComponents *tomorrowComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:nextDate];
NSInteger tomorrowDay = [tomorrowComponents day];
NSInteger tomorrowMonth = [tomorrowComponents month];
NSInteger tomorrowYear = [tomorrowComponents year];

[gregorian release];


// now build the predicate needed to fetch the information
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"DateAttribute < %@ && DateAttribute > %@", nextDate, thisDate];

此谓词将准确检索 DateAttribute 属于当天的所有对象。希望这会有所帮助。

关于iphone - 使用核心数据按工作日分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1260014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com