1

I'm confused about this code:

for (int i = 0; i < [content count]; i++) {
    NSString *dateString = [self replaceCharacters:[[[content objectAtIndex:i] objectForKey:@"date"] objectForKey:@"text"]];
    NSLog(@"\n\n%i",[[dateFormatter stringFromDate:[originalDate dateFromString:[[[content objectAtIndex:i] objectForKey:@"date"] objectForKey:@"text"]]] intValue]);
    if ([[dateFormatter stringFromDate:[originalDate dateFromString:dateString]] intValue] >= [todayString intValue]) {
        [tempContent addObject:[content objectAtIndex:i]];
    }

}

I'll explain:

I need to check if the date of every object in contents is higher or equal to the today date. If it's so, it will be added to the tempContent. But the tempContent count is equal to the content count. Nothing got sorted...

Does anyone see the solution?!

Thanks, mavrick3.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
  • So, to clarify, you want to add the dates that are in the future to tempContent? Are you including the time in the comparison, or only the date? – Ned Feb 22 '11 at 17:39

3 Answers3

1

Why don't you use NSDates -compare: method? It returns you the constant NSOrderedDescending, if the first date is later in time, NSOrderedAscending if it is earlier and you will find out yourself when it returns NSOrderedSame.

For example:

if ([originalDate compare:todayDate] == NSOrderedDescending) {
        [tempContent addObject:[content objectAtIndex:i]];
}
Björn Marschollek
  • 9,899
  • 9
  • 40
  • 66
  • I used this `if ([[originalDate dateFromString:dateString] compare:today] == (NSOrderedDescending || NSOrderedSame)) { [tempContent addObject:[content objectAtIndex:i]]; }` but it doesn't work for dates which are equal to today date... – Fabio Poloni Feb 25 '11 at 19:32
  • `||` is the bitwise or operator. If you write it that way, you apply it to the constants, not to the comparison results. Try `if ([[originalDate dateFromString:dateString] compare:today] == NSOrderedDescending || [[originalDate dateFromString:dateString] compare:today] == NSOrderedSame) { [tempContent addObject:[content objectAtIndex:i]]; }` – Björn Marschollek Feb 25 '11 at 23:11
  • I tried to remember it this way: if you have `[A compare:B]`, try to imagine that it's a sequence of two integers `A B` read from left to right. If `A = 5` and `B = 3`, then the sequence is `5 3`; therefore, the sequence is called **descending** (`NSOrderedDescending`). – dvdchr May 27 '15 at 14:48
0

You want to use NSDateComponents, instead of all of that stringFromDate stuff.

See this answer for more info.

But basically you will want something like:

unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* origComponents = [calendar components:flags fromDate:originalDate];
NSDate* origDateOnly = [calendar dateFromComponents:origComponents];

for (int i = 0; i < [content count]; i++) 
{
    NSDate *date = [[content objectAtIndex:i] objectForKey:@"date"];
    NSDateComponents* components = [calendar components:flags fromDate:date];
    NSDate* dateOnly = [calendar dateFromComponents:components];

    if ([origDateOnly compare:dateOnly] == (NSOrderedSame || NSOrderedAscending)) 
    {
         [tempContent addObject:[content objectAtIndex:i]];
    }
}
Community
  • 1
  • 1
Ned
  • 6,280
  • 2
  • 30
  • 34
  • If I use this I get some errors... I know that I need to add some stuff... But the whole code is buggy... – Fabio Poloni Feb 22 '11 at 17:55
  • I fixed one bug I found in my code but since you didn't provide much info in your original code (like the structure of your "content" object), I just typed something up quick in the browser and didn't really test it. The main idea is using the date components instead of a date->string->int conversion to compare is much cleaner and easier to understand. – Ned Feb 23 '11 at 04:38
0

Have a look on Erica Sadun's NSDate-Extensions. It has lots of useful methods like isEqualToDateIgnoringTime: and isLaterThanDate:

This helped me solving problems similar to yours.

Gurnetko
  • 76
  • 4