NSFetchedResultsController,日期为谓词

时间:2012-11-08 08:09:16

标签: ios core-data nsfetchedresultscontroller

情景:

我有跟踪iOS应用程序的费用,我将费用从费用明细视图控制器存储到表视图(带有提取结果控制器),该视图显示费用列表以及类别,金额和日期。我的实体“Money”中有一个日期属性,它是费用或收入的父实体。

问题:

我想要的是基本上对给定的一周,一个月或一年的费用进行分类,并将其显示为部分标题标题,例如:(10月1日至10月7日),它显示费用金额和相关的东西根据那个特定的一周。在该视图中提供了两个按钮,如果我按下右键,它将使一周增加一周(2012年10月1日至10月7日现在显示2012年10月8日 - 10月15日),同样左按钮将减少一周一个星期。

我的问题是如何实现上述目标。我写了一些伪代码 - 任何帮助将不胜感激。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date == %@)", dateToFilterFor];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];

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

为了过滤一周,谓词应该是什么?另外,我想在获取的结果控制器中有一个节名,但它会是什么?

1 个答案:

答案 0 :(得分:0)

假设用户选择了10月1日和10月7日,

NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy"];

NSString *str1 = @"10/01/2012";
NSDate *date1 = [formatter dateFromString:str1];

NSString *str2 = @"10/07/2012";
NSDate *date2 = [formatter dateFromString:str2];

将其用作,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", date1, date2];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];

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

为了添加日期,

int addDaysCount = 7; //try with negative values for fetching previous week.

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:addDaysCount];

NSDate *newDate1 = [[NSCalendar currentCalendar] 
                              dateByAddingComponents:dateComponents 
                                              toDate:date1 options:0];
NSDate *newDate2 = [[NSCalendar currentCalendar] 
                              dateByAddingComponents:dateComponents 
                                              toDate:date2 options:0];

将上述谓词与newDate1newDate2一起使用,以获取与这些日期对应的新费用。

如果您想获得特定周的星期日,请使用NSDateComponents

NSDateComponents *components = [NSDateComponents new]; 
components.weekday = 1;//represents sunday 
components.year = 2012; 
components.weekOfMonth = 1;
components.month = 10; //etc..

NSDate *date1 = [[NSCalendar currentCalendar] dateFromComponents: components]; 

more details on NSDateComponents and its properties check