【问题标题】:search by date in coredata在 coredata 中按日期搜索
【发布时间】:2011-07-19 17:13:35
【问题描述】:

如何预测上周、昨天最后一个月插入的所有数据

我的核心数据中有 nsdate 字段,但我不知道如何使用它进行搜索

【问题讨论】:

    标签: objective-c core-data


    【解决方案1】:

    我想您设置了一个 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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多