我陷入了一个谓词
我需要获取在给定时段内发生的事件数组。 如果给定日期是7月份.i.e。 7月1日至31日..然后我需要event1,事件2,事件3,事件5,事件6
所以我有事件startDate和endDate
Event 1 - June 10 - July 14
Event 2 - July 4
Event 3 - July 7
Event 4 - June 9
Event 5 - July 1-July 3
Event 6 - May 1 - Dec 10
我试过以下但它只会返回event2,event3
[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ >= %%@ AND %@ < %%@", @"startTime", @"endTime"], startDate, endDate]
答案 0 :(得分:0)
试试这个
NSDate *now = [NSDate date];
NSInteger month = 7; // given month
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setDay:0];
[components setMonth:month];
NSDateComponents *components1 = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components1 setDay:31];
[components1 setMonth:month];
NSDate *startDate = [calendar dateFromComponents:components];
NSDate *endDate = [calendar dateFromComponents:components1];
NSPredicate* pred = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date =< %@)", startDate, endDate];
NSArray* filteredArray = [yourarray filteredArrayUsingPredicate:pred]; // your array