0

NSDate オブジェクトを含む NSMutableArray "NewDate" があります

(
"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);

しかし、「結果」を作成するときにエラーが発生します。

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDate 0x72a0ee0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key startDate.'

私は何を間違っていますか?助けてくれてありがとう。

4

2 に答える 2

1

次のコードで試してください。

array={"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"};/////例。

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]];
}
}
于 2013-08-12T10:47:33.373 に答える