Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何检查NSDate是否在特定月份?_Ios_Objective C_Nsdate_Nspredicate - Fatal编程技术网

Ios 如何检查NSDate是否在特定月份?

Ios 如何检查NSDate是否在特定月份?,ios,objective-c,nsdate,nspredicate,Ios,Objective C,Nsdate,Nspredicate,我有一个NSDate数组,我试图做的是检查日期是否在一年中的特定月份,并将它们组合在另一个数组中。我获得了NSInteger的年薪。如果你们能帮助我使用谓词或简单的if()语句,这将对我有很大帮助。你们可以使用下面的函数并为数组生成for循环 for(NSDate *date in arrDates){ if([self isDateFalls:date withYourYear:year andYourMonth:month]){ NSLog(@"YES DATE FAL

我有一个NSDate数组,我试图做的是检查日期是否在一年中的特定月份,并将它们组合在另一个数组中。我获得了NSInteger的年薪。如果你们能帮助我使用谓词或简单的if()语句,这将对我有很大帮助。

你们可以使用下面的函数并为数组生成for循环

for(NSDate *date in arrDates){

  if([self isDateFalls:date withYourYear:year andYourMonth:month]){
        NSLog(@"YES DATE FALLS IN YEAR AND MONTH");
  } 
  else{
        NSLog(@"NO DATE NOT FALLS IN YEAR AND MONTH");
  }
}

-(BOOL)isDateFalls:(NSDate *)date withYourYear:(NSInteger)year andYourMonth:(NSInteger)month{

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [gregorian components:(NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:date];
    if(dateComponents.year == year && dateComponents.month == month){

        return YES;
    }

    return NO;

}

为什么不对NSDates数组进行排序,然后循环遍历它,检查每个日期以查看它所在的月份/年份,并将它们放入NSDictionary中?标准方法是使用NSCalendar/NSDateComponents从每个日期提取(数字)月份。