【发布时间】:2011-07-19 17:13:35
【问题描述】:
如何预测上周、昨天最后一个月插入的所有数据
我的核心数据中有 nsdate 字段,但我不知道如何使用它进行搜索
【问题讨论】:
标签: objective-c core-data
如何预测上周、昨天最后一个月插入的所有数据
我的核心数据中有 nsdate 字段,但我不知道如何使用它进行搜索
【问题讨论】:
标签: objective-c core-data
我想您设置了一个 fetchedResultsController 来从数据库中检索对象。 设置 fetchedResultsController 时,您必须在 fetchRequest 中添加 NSPredicate。
日期无耻地从here盗取
//You can set up your custom dates like this
NSDate *today = [NSDate date];
NSDate *yesterday = [today addTimeInterval: -86400.0];
NSDate *thisWeek = [today addTimeInterval: -604800.0];
NSDate *lastWeek = [today addTimeInterval: -1209600.0];
// This predicate retrieves all the object created since last week.
NSpredicate *predicate = [NSPredicate predicateWithFormat:@"yourDateField >= %@ ", lastWeek];
[fetchRequest setPredicate:predicate];
// You can add sorting like this
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"yourDateField" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
【讨论】: