【问题标题】:CoreData - NSPredicate formated with time range (NSTimeInterval)核心数据 - 具有时间范围的 NSPredicate 格式 (NSTimeInterval)
【发布时间】:2009-07-08 16:00:36
【问题描述】:

我正在尝试找出如何浏览我的 CoreData 信息并找到在 NSTimeInterval 内具有 createdAt(作为 NSDate 的我的对象的一部分)的对象。我该如何设置?

我查看了以下文档: http://developer.apple.com/documentation/Cocoa/Conceptual/Predicates/predicates.html

但我没有找到任何东西。

我需要创建两个时间戳并使用 SQL 的 BETWEEN 吗?

任何帮助都会很棒。

【问题讨论】:

    标签: sqlite nsdate


    【解决方案1】:

    首先,检查 NSDate 是否在 NSTimeInterval 内是没有意义的,因为 NSTimeInterval 只是指定时间长度,而不是它的位置。相反,您想使用两个单独的 NSDates 指定间隔的开始和结束。

    这是它的样子(beginningTime 和 endTime 是 NSDates)。

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"YourEntityName" inManagedObjectContext:yourContext];
    NSPredicate *beginningPredicate = [NSPredicate predicateWithFormat:@"createdAt >= %@", beginningTime];
    NSPredicate *endPredicate = [NSPredicate predicateWithFormat:@"createdAt <= %@", endTime];
    request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:beginningPredicate, endPredicate, nil]];
    NSArray *results = [yourContext executeFetchRequest:request error:NULL];
    

    【讨论】:

      猜你喜欢
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多