Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用核心数据按工作日分组_Iphone_Objective C_Cocoa Touch_Iphone Sdk 3.0_Core Data - Fatal编程技术网

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

Iphone 使用核心数据按工作日分组,iphone,objective-c,cocoa-touch,iphone-sdk-3.0,core-data,Iphone,Objective C,Cocoa Touch,Iphone Sdk 3.0,Core Data,在我的核心数据模型中,我有一个具有日期属性的实体,正如标题所示,我希望按(周)天对该实体进行分组 问题是,日期或多或少都存储为时间戳,我不知道如何创建一个能够适当分组/过滤实体的谓词 我发现我可能每天都要进行一次提取,所以创建了以下方法。 我需要帮助的代码就在中间。 - (NSFetchedResultsController *)fetchedResultsController:(NSDate *)day { if(fetchedResultsController != nil) re

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

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

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

- (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;
-(NSFetchedResultsController*)fetchedResultsController:(NSDate*)天{
if(fetchedResultsController!=nil)
返回获取的ResultsController;
//创建和配置请求
NSFetchRequest*request=[[NSFetchRequest alloc]init];
NSEntityDescription*实体=[NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext];
[请求集合实体:实体];
//谓词
//我不知道的伪代码用“-start”标记
NSPredicate*谓词=[NSPredicate predicateWithFormat:@“介于和之间的DateAttribute];
//或
NSPredicate*谓词=[NSPredicate谓词格式:@“LIKE”];
[请求集谓词:谓词];
//我不懂的伪代码用“-end”标记
//排序描述符
NSSortDescriptor*titleDescriptor=[[NSSortDescriptor alloc]initWithKey:SortDescriptor名称升序:是];
NSArray*sortDescriptors=[NSArray阵列WithObject:titleDescriptor];
[请求集合描述符:排序描述符];
//创建并初始化fetchResultsController
NSFetchedResultsController*aFetchedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@“Root”];
self.fetchedResultsController=aFetchedResultsController;
fetchedResultsController.delegate=self;
//记忆
[请求释放];
[标题描述者发布];
[aFetchedResultsController释放];
返回获取的ResultsController;
}


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

如果您想按工作日分组,最简单的方法是在实体中添加一个属性,如“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];
//首先检索今天的日期、工作日、月份和年份组件
NSCalendar*gregorian=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents*todayComponents=[公历成分:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit)fromDate:[NSDate日期]];
NSInteger theDay=[今天];
NSInteger theMonth=[Today Components month];
NSInteger theYear=[Today Components year];
//现在,使用这些组件为输入日期构建NSDate对象
NSDateComponents*components=[[NSDateComponents alloc]init];
[组件设置日期:theDay];
[组件设置月份:每月];
[组件设置年份:年份];
NSDate*thisDate=[gregorian dateFromComponents:components];
[组件发布];
//现在为明天构建一个NSDate对象
NSDateComponents*offsetComponents=[[NSDateComponents alloc]init];
[抵销组件设定日:1];
NSDate*nextDate=[gregorian Date ByAddingComponents:offsetComponents toDate:thisDate选项:0];
[抵消组件发布];
NSDateComponents*tomorrowComponents=[公历组件:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit)fromDate:nextDate];
NSInteger明日=[明日组件日];
NSInteger-tomorrowMonth=[明天组件月];
NSInteger-tomorrowYear=[明天组件年];
[公历释放];
//现在构建获取信息所需的谓词
NSPredicate*谓词=[NSPredicate谓词格式:@“DateAttribute<%@&&DateAttribute>%@”,下一个日期,这个日期];
此谓词将准确检索DateAttribute位于当前日期内的所有对象。希望这有帮助。

嗨@Gabbutb 我实际上是在看你的问题,我有一个类似的问题。 我想做的是将我的表按日期划分为多个部分,而不是按你提到的时间戳。因此,我在类中添加了一个新方法,该方法扩展了NSManagedObject(我正在获取的实体),该方法返回格式化的日期:

- (NSString *)transactionDay{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
return [dateFormatter stringFromDate:self.date];}
然后将我的fetchedResultController更改为:

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"transactionDay" cacheName:nil];
一切都很有魅力

查看链接以查看更多详细信息。

感谢您的全面回复!“如果要按天分组,则在初始化NSFetchedResultsController时将DateAttribute作为sectionNameKeyPath参数传递。同样,无需指定谓词:您将自动获取天数的节。”如果这样做有效,那将非常好,但在这样做时,它会按DateAttribute的值分组(包括时间,例如“2009-07-02 20:51:27-0400”)备注:我的最终目标是获得一个工作日列表,当单击其中一个工作日时,与该工作日相关的所有实体都会在另一个tableview中打开。您可以通过在实体中存储DateAttribute而无需时间来实现这一点,方法与我在示例代码中展示的相同:从NSDate对象(您的DateAttribute)开始,首先提取其日期成分