我試圖比較日期,但它不能與2012年1月1日比較像31日 - 12月2011日。我試圖在2011年12月31日以及2012年1月1日之前刪除任何其他值。這裏我是這樣做的:我想在今天的日期和即將到來的日期顯示值。比較日期
-(void)showcurrentdate
{
NSDate *objdate=[NSDate date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"dd-MM-YYYY"];
NSDate *str1= (NSDate *)[dateformatter stringFromDate:objdate];
NSLog(@"configuregameArray......%d",[configuregameArray count]);
if([configuregameArray count]>=1)
{
NSMutableArray* indexarray= [[NSMutableArray alloc]init];
for(int k =0; k<[configuregameArray count]; k++)
{
NSDate *datefromarray = (NSDate *)[[configuregameArray objectAtIndex:k] objectForKey:@"gd"];
NSComparisonResult result = [datefromarray compare:str1];
switch (result)
{
case NSOrderedAscending:
NSLog(@"%@ is greater from %@", str1 ,datefromarray);
indexNumber = [NSString stringWithFormat:@"%d",k];
[indexarray addObject:indexNumber];
NSLog(@"indexarray......%@",indexarray);
break;
default:
NSLog(@"going fine");
break;
}
}
NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet];
for(int i=0; i<[indexarray count]; i++)
{
NSInteger removeindex = [[indexarray objectAtIndex:i]intValue];
[discardedItems addIndex:removeindex];
}
[configuregameArray removeObjectsAtIndexes:discardedItems];
}
NSLog(@"configuregameArray......%@",configuregameArray);
}
您是否有日期數組,並且只想過濾出某個日期之後的日期,例如: 2012年1月1日?你可以使用-indexesOfObjectsPassingTest過濾這些:(如果我正確理解問題,我會寫一個答案) – 2011-12-30 09:20:51
@DavidRönnqvist其實我有一個數組(configuregamearay),我把日期作爲字符串存儲在鍵值'gd'中。我在'datefromarray'中獲取相同的內容並與當前日期進行比較。我想從具有日期早於當前日期的configuregamearay中刪除值。 – Ketan 2011-12-30 09:39:24
那麼,'[[configuregameArray objectAtIndex:k] objectForKey:@「gd」]'實際上給你一個'NSDate'還是一個'NSString'? – DarkDust 2011-12-30 09:44:41