i am trying to compare the date but its not comparing the date like 31-dec-2011 with 1-01-2012. i was trying to remove values at 31-dec-2011 and any other values previous then 1-01-2012. here i am doing this: i want to show the values at today’s date and on upcoming dates.
-(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);
}
Given that you have your dates stored as strings in another array and that you want to end up with an array of strings, filtering out only the dates that occur after a certain date would take these steps:
Obviously it would be better to store dates in the array. This make all date calculations much easier. Then you could filter the array with a one line NSPredicate instead of many lines of code. It is generally a bad design to store objects as strings. A much better practice is to store the objects as objects and convert them to strings when presenting them in a user interface. This was you can change how the objects are formatted without having to change how they are stored, converted, and used. It is a separation of concerns where the interface who is concerned about showing dates as strings converts dates to strings while the model who is concerned about dates uses dates and doesn’t care about strings at all.
Anyhow, code for the above steps to filter the date strings in your question would look something like below. If you want to filter using todays date you can use
[NSDate date];to get todays date as a date and not a string.