我被困在一个谓词
我需要在给定的月份期间获取事件数组。如果给定的日期是7月。1-31st 7月. .然后我需要event1, event 2, event 3, event 5, event6
所以我有事件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]
试试这个
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