NSPredicate - 用于查找在特定日期范围之间发生的事件 [英] NSPredicate- For finding events that occur between a certain date range

查看:290
本文介绍了NSPredicate - 用于查找在特定日期范围之间发生的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个更复杂的只是

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startDate >= %@ AND endDate <= %@", startDay,endDay];

我正在建立一个日历应用程式,我需要将事件从核心资料库在某一天。但是,在我尝试显示的日期的某一天,某个活动可能开始/结束,例如

I'm building a calendar app and I need to pull events out of my core data store that occur on a given day. However, it's possible for an event to start/end on a different day then the day I'm trying to display, e.g.

我的日期范围(开始= 2011-12 -02 00:00:00 to End = 2011-12-02 23:59:59)

My Date Range (Start = 2011-12-02 00:00:00 to End = 2011-12-02 23:59:59)

活动(开始= 2011-12-01 23:00: 00 to End = 2011-12-02 05:00:00)

An Event (Start = 2011-12-01 23:00:00 to End = 2011-12-02 05:00:00)

如何编写谓词以确定该事件是否落在该日期范围内。请记住,活动可在日期范围之前和之后开始。

How can I write a predicate to determine if that event falls in that date range. Keep in mind that an event could start before and after the date range.

谢谢!

推荐答案

这里是一个方法来建立一个谓词来检索在某一天发生的非重复事件(重复事件需要额外的处理):

Here is a method to build a predicate to retrieve non recurring events occurring on a given day (recurring events require additional processing):

- (NSPredicate *) predicateToRetrieveEventsForDate:(NSDate *)aDate {

    // start by retrieving day, weekday, month and year components for the given day
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:aDate];
    NSInteger theDay = [todayComponents day];
    NSInteger theMonth = [todayComponents month];
    NSInteger theYear = [todayComponents year];

    // now build a NSDate object for the input date using these components
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:theDay]; 
    [components setMonth:theMonth]; 
    [components setYear:theYear];
    NSDate *thisDate = [gregorian dateFromComponents:components];
    [components release];

    // build a NSDate object for aDate next day
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setDay:1];
    NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
    [offsetComponents release];

    [gregorian release];


    // build the predicate 
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"startDate < %@ && endDate > %@", nextDate, thisDate];

        return predicate;

}

谓词几乎等于@Tony ,除了它不检查相等。这是为什么。假设您的活动始于12月8日23:00,结束于12月9日00:00。如果您检查的结束日期是> =而不是某一天的事件,您的应用程序将在12月8日和9日报告该事件,这是明显错误。尝试将这样的事件添加到iCal和Google日历,您将看到该事件只出现在12月8日。在实践中,您不应该假设某一天在23:59:59结束(尽管这当然是真):你需要把午夜当作一天的最后一天(关于一个事件的结束)。此外,请注意,这不会阻止从午夜开始的事件。

The predicate is almost equal to the one proposed by @Tony, except it does not check for equality. Here is why. Suppose you have an event starting on December 8 23:00 and ending at December 9 00:00. If you check for events whose ending date is >= rather than > of the given day, your app will report the event in both December 8 and 9, which is clearly wrong. Try adding such an event to both iCal and Google Calendar, and you will see that the event only appears on December 8. In practice, you should not assume that a given day ends at 23:59:59 (even though this is of course true): you need to treat midnight as the last second of a given day (with regard to the end of an event). Also, note that this does not prevent events starting at midnight.

这篇关于NSPredicate - 用于查找在特定日期范围之间发生的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆