【问题标题】:predicateForEventsWithStartDate function : Is this a bug?predicateForEventsWithStartDate 函数:这是一个错误吗?
【发布时间】:2012-12-10 07:21:00
【问题描述】:

当使用 predicateForEventsWithStartDate 函数时,看起来返回的事件位于大约 4 年的时间间隔内。这是一个错误吗?

【问题讨论】:

    标签: iphone objective-c ios ios5


    【解决方案1】:

    是的,这似乎是一个错误,我也有同样的经历。 predicateForEventsWithStartDate: 仅返回从开始日期起 4 年时间间隔内的事件。超出时间间隔的所有其他事件都不会返回。

    【讨论】:

    • 哈哈!我也在 iOS 7 上遇到了这种情况。我目前的解决方法是在 [-1 年,+3 年] 内获取事件。
    【解决方案2】:

    您可以通过选择间隔来返回事件(选择开始日期和结束日期):

    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:[NSDate date]];
    [components setDay:1];
    [components setMonth:1];
    [components setYear :[components year] - 1 ];
    NSDate *startDate = [CURRENT_CALENDAR dateFromComponents:components];
    
    NSDateComponents *EndComponents = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:[NSDate date]];
    [EndComponents setDay:1];
    [EndComponents setMonth:1];
    [EndComponents setYear:[EndComponents year] + 1];
    NSDate *endDate = [CURRENT_CALENDAR dateFromComponents:EndComponents];
    
    NSArray *calendarArray = nil;
    if(systemVersion < 6.0)
    {
         calendarArray = [yourEventStore calendars];
    }
    else
    {
         calendarArray = [yourEventStore calendarsForEntityType:EKEntityTypeEvent];
    }
    NSPredicate *predicate = [yourEventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];
    [arrayOfEvents addObjectsFromArray:[yourEventStore eventsMatchingPredicate:predicate]];
    

    【讨论】:

    • @Mohammad Rabi:检索事件的方法是正确的,但返回的值是从开始日期算起的 4 年间隔内。你检查时间间隔了吗?
    • 我编辑了上面的答案,例如您可以获取从 2011 年 1 月 1 日到 2013 年 1 月 1 日的事件
    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 2012-03-25
    • 2010-12-22
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多