Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.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 从日期数组中获取具有当前日期的日期数组时出错_Ios_Nsarray_Nsdate_Nspredicate - Fatal编程技术网

Ios 从日期数组中获取具有当前日期的日期数组时出错

Ios 从日期数组中获取具有当前日期的日期数组时出错,ios,nsarray,nsdate,nspredicate,Ios,Nsarray,Nsdate,Nspredicate,我有一个NSMutableArray“NewDate”和NSDate对象 ( "2013-08-11 20:26:11 +0000", "2013-08-11 20:26:11 +0000", "2013-08-12 06:22:38 +0000", "2013-08-12 07:24:14 +0000", "2013-08-12 08:04:05 +0000" ) 我想用今天(8月12日)的对象从这个数组中创建一个新数组 我的代码: NSCalendar *calendar = [NSCal

我有一个NSMutableArray“NewDate”和NSDate对象

(
"2013-08-11 20:26:11 +0000",
"2013-08-11 20:26:11 +0000",
"2013-08-12 06:22:38 +0000",
"2013-08-12 07:24:14 +0000",
"2013-08-12 08:04:05 +0000"
)
我想用今天(8月12日)的对象从这个数组中创建一个新数组

我的代码:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
[components setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *firstDate = [calendar dateFromComponents:components];
[components setMonth:0];
[components setDay:1];
[components setYear:0];
NSDate *secondDate = [calendar dateByAddingComponents:components toDate:firstDate options:0];

NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:@"startDate > %@", firstDate];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"startDate < %@", secondDate];

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:firstPredicate, secondPredicate, nil]];

NSMutableArray *results = [newDate filteredArrayUsingPredicate:predicate];
NSLog(@"%@",result);
NSCalendar*日历=[NSCalendar当前日历];
NSDateComponents*components=[日历组件:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)fromDate:[NSDate日期]];
[组件设置时区:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate*firstDate=[calendar dateFromComponents:components];
[组件设置月份:0];
[组件设定日:1];
[组件设置年份:0];
NSDate*secondDate=[calendar Date ByAddingComponents:components toDate:firstDate选项:0];
NSPredicate*firstPredicate=[NSPredicate predicateWithFormat:@“startDate>%@”,firstDate];
NSPredicate*secondPredicate=[NSPredicate predicateWithFormat:@“startDate<%@”,secondDate];
NSPredicate*谓词=[NSCompoundPredicate和PredicateWithSubPredicates:[NSArray arrayWithObjects:firstPredicate,secondPredicate,nil]];
NSMutableArray*结果=[newDate FilteredArray使用谓词:谓词];
NSLog(@“%@”,结果);
但在创建“结果”时,它会给我带来错误:

***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[valueForUndefinedKey:]:此类不符合密钥起始日期的键值编码。”
我做错了什么? 谢谢你的帮助。

试试这个

- (NSComparisonResult)compareDateOnly:(NSDate *)otherDate {
    NSUInteger dateFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
    NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *selfComponents = [gregorianCalendar components:dateFlags fromDate:self];
    NSDate *selfDateOnly = [gregorianCalendar dateFromComponents:selfComponents];

    NSDateComponents *otherCompents = [gregorianCalendar components:dateFlags fromDate:otherDate];
    NSDate *otherDateOnly = [gregorianCalendar dateFromComponents:otherCompents];
    return [selfDateOnly compare:otherDateOnly];
}

尝试以下代码

数组={“2013-08-1120:26:11+0000”, "2013-08-11 20:26:11 +0000", "2013-08-12 06:22:38 +0000", "2013-08-12 07:24:14 +0000", "2013-08-12 08:04:05 +0000"};/////例如

NSMutableArray *newArray=[[NSMutableArray alloc]init];
NSDate *today=[NSDate date];

NSComparisonResult result;
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending

for(int i=0;i<array.count;i++)
{
result = [today compare:[array objectAtIndex:i]];


if(result==NSOrderedDescending)//////If matches with today's date.
{
   [newArray addObject:[array objectAtIndex:i]];
}
}
NSMutableArray*newArray=[[NSMutableArray alloc]init];
NSDate*今天=[NSDate日期];
比较结果;
//有三个可能的值:nsOrderedName、nsOrderedescending、nsOrderedescending

对于(inti=0;iWell,首先,您正在创建一个名为“startDate”的完全虚构的属性您试图对第一个和第二个谓词执行什么操作?我不知道您是否询问过这一问题,但如果您只想比较NSDate的日期组件,则必须使用NSCalendar和NSDateComponents删除时间组件。类似的内容应作为NSDate的一个类别:
NSMutableArray *newArray=[[NSMutableArray alloc]init];
NSDate *today=[NSDate date];

NSComparisonResult result;
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending

for(int i=0;i<array.count;i++)
{
result = [today compare:[array objectAtIndex:i]];


if(result==NSOrderedDescending)//////If matches with today's date.
{
   [newArray addObject:[array objectAtIndex:i]];
}
}