nspreate用于多日全天事件



在我的数据库中,我有两列即startDate和endDate,如果用户在UI中按下任何日期,如2015年7月16日,那么它应该获取startDate和endDate之间的所有事件。

前面我写了一个谓词来获取起始事件:

[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ >= %%@ AND %@ < %%@", @"startTime", @"endTime"], date, [date dateForNextDay]]

但是如果给定的日期不是事件的开始日期,上述查询将不起作用。

的例子:如果我有一个会议日期为7/13 - 7/16和给定的日期是7/14,那么会议事件应该能够使用谓词获取。我的旧查询将只在7/13工作。它应该在给定的日期获取事件——7/13、7/14、7/15、7/16

获取给定日期大于等于或小于等于给定日期的事件....

[NSPredicate predicateWithFormat:@"%@ >= startTime AND %@ <= endTime", date, date]

编辑……用一个简单的对象测试它,确认它正确工作....

- (NSArray *)filterArray:(NSArray *)array forDate:(NSDate *)date {
    NSPredicate *p = [NSPredicate predicateWithFormat:@"%@ >= startTime AND %@ <= endTime", date, date];
    return [array filteredArrayUsingPredicate:p];
}
- (void)testPredicate {
    NSDictionary *event0 = @{ @"startTime":[self dateFromString:@"07/01/2015"], @"endTime":[self dateFromString:@"07/04/2015"] };
    NSDictionary *event1 = @{ @"startTime":[self dateFromString:@"07/10/2015"], @"endTime":[self dateFromString:@"07/14/2015"] };
    NSDictionary *event2 = @{ @"startTime":[self dateFromString:@"08/02/2015"], @"endTime":[self dateFromString:@"08/09/2015"] };
    NSArray *events = @[event0, event1, event2];
    NSDate *july9 = [self dateFromString:@"07/09/2015"];
    NSDate *july10 = [self dateFromString:@"07/10/2015"];
    NSDate *july11 = [self dateFromString:@"07/11/2015"];
    NSDate *july13 = [self dateFromString:@"07/13/2015"];
    NSDate *july14 = [self dateFromString:@"07/14/2015"];
    NSDate *july15 = [self dateFromString:@"07/15/2015"];
    NSLog(@"filtered using 9 should be empty %@", [self filterArray:events forDate:july9]);
    NSLog(@"filtered using 10 should hit %@", [self filterArray:events forDate:july10]);
    NSLog(@"filtered using 11 should hit %@", [self filterArray:events forDate:july11]);
    NSLog(@"filtered using 13 should hit %@", [self filterArray:events forDate:july13]);
    NSLog(@"filtered using 14 should hit %@", [self filterArray:events forDate:july14]);
    NSLog(@"filtered using 15 should be empty %@", [self filterArray:events forDate:july15]);
}
- (NSDate *)dateFromString:(NSString *)string {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
    return [dateFormatter dateFromString:string];
}

上述代码不需要对午夜情况进行特殊处理,以便按照问题的建议执行(包括最后一个endTime)。

这会生成以下日志输出,您可以自己确认…

filtered using 9 should be empty ()
filtered using 10 should hit (
        {
        endTime = "2015-07-14 07:00:00 +0000";
        startTime = "2015-07-10 07:00:00 +0000";
    }
)
filtered using 11 should hit (
        {
        endTime = "2015-07-14 07:00:00 +0000";
        startTime = "2015-07-10 07:00:00 +0000";
    }
)
filtered using 13 should hit (
        {
        endTime = "2015-07-14 07:00:00 +0000";
        startTime = "2015-07-10 07:00:00 +0000";
    }
)
filtered using 14 should hit (
        {
        endTime = "2015-07-14 07:00:00 +0000";
        startTime = "2015-07-10 07:00:00 +0000";
    }
)
filtered using 15 should be empty ()

我不明白为什么你有两个日期:date和dateForNextDay。

我想你应该试试这个:[NSPredicate predicateWithFormat:@"startTime>= %@ &&% @ & lt;endTime", date, [date dateForNextDay]];

或者把最后一个%@%%@交换一下,像这样=>@"%@>= %%@ AND %%@ <% @"

编辑:顺便说一句,正如@danh所说,也许你只是想用日期,日期。

编辑2:好吧,我回答得太快了,all条件为假,danh又对了;)@"startTime <= %@ &&%@ <= endTime"

相关内容

  • 没有找到相关文章

最新更新