NSPredicate - 获取仅限于未来日期的核心数据

时间:2012-03-06 13:33:04

标签: core-data nspredicate

我需要检索Booking属性设置为当前日期或未来日期的所有departDate个对象。我的代码如下:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(departDate >= %@)", [NSDate date]];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
NSError *error;
bookings = [[context executeFetchRequest:fetchRequest error:&error] retain];

NSLog(@"curernt date %@", [NSDate date]);

for (Booking *order in bookings) {
    NSLog(@"%@", [NSDate dateWithTimeIntervalSince1970:order.departDate]);
}

但是日志显示:

2012-03-06 18:58:24.518 Airline[2201:207] curernt date 2012-03-06 13:28:24 +0000
2012-03-06 18:58:24.520 Airline[2201:207] 2012-03-06 12:37:59 +0000
2012-03-06 18:58:24.528 Airline[2201:207] 2012-03-05 12:55:50 +0000
2012-03-06 18:58:24.529 Airline[2201:207] 2012-03-08 12:55:50 +0000
2012-03-06 18:58:24.530 Airline[2201:207] 2012-03-02 13:07:33 +0000
2012-03-06 18:58:24.531 Airline[2201:207] 2012-04-06 13:13:53 +0000
2012-03-06 18:58:24.532 Airline[2201:207] 2012-01-06 13:13:53 +0000

正在检索所有对象,就好像没有过滤器一样。有人可以告诉我这里我做错了吗?

1 个答案:

答案 0 :(得分:2)

核心数据支持直接日期为NSDate。您使用NSDate和日期为1970年以来的秒进行混音

在你的模型中,make sue departDate设置为“Date”,然后使用

for (Booking *order in bookings) {
    NSLog(@"%@", order.departDate);
}