views:

405

answers:

2

Hi

Well I guess this has been asked a thousand times, but for some reason the answeres dont really work or had other problems,....

Anyway here is what I have "working" :

    NSCalendar *calendar = [NSCalendar currentCalendar];    
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    // set tomorrow (0: today, -1: yesterday)
    [comps setDay:0];
    NSDate *dateToday = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    [comps setDay:-1];
    NSDate *dateYesterday = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    [comps release];


NSString *todayString = [self.dateFormatter stringFromDate:dateToday] ;
NSString *yesterdayString = [self.dateFormatter stringFromDate:dateYesterday] ;
NSString *refDateString = [self.dateFormatter stringFromDate:info.date];

if ([refDateString isEqualToString:todayString]) 
{
    cell.title.text =  @"Today";
} else if ([refDateString isEqualToString:yesterdayString]) 
{
    cell.title.text =  @"Yesterday";
} else 
{
    cell.title.text =  [self.dateFormatter stringFromDate:info.date];
}

Now to the problem(s) :

That seems to be an awefull lot of code for just a date comparinson, is there an easier way ?

And the most important question is the release of all the objects. As might have guessed, I use this in a UITableViewController. I also have these lines in my code :

//[calendar release];
//[currentDate release];
//[dateToday release];
//[dateYesterday release];
//[todayString release];
//[yesterdayString release];
//[refDateString release];

The problem is that as soon that I uncomment one of these lines, my app crashes and I have no idea why ?! I hope someone can enlighten me here.

Thanks lot.

A: 

You can compare two NSDate objects using the isEqualToDate method, will return a bool (yes or no) telling you if the dates are equal. See Apple Documentation.

As for the releasing the objects - the rule in objective-c is that you only release objects that you alloc memory for during the course of your method. The only object you're specifically allocating memory for in your code is comps so it's the only one you should release - and you've done so. Releasing memory for objects you never allocated is probably what is causing your app to crash. When the methods which actually allocated those objects attempts to release them or another reference to them tries to access them, they'll trigger an EXC_BAD_ACCESS error as you've already released them!

So don't just comment out those release lines, delete them from your script - there's no need for them!

JoeR
`isEqualToDate:` won't work here. It only returns the `YES` if the dates are "exactly" the same: "This method detects sub-second differences between dates." (From the class reference.)
Mo
+2  A: 

The easiest way to do it is to just compare the description of the dates:

// Your dates:
NSDate * today = [NSDate date];
NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-86400]; //86400 is the seconds in a day
NSDate * refDate; // your reference date

// 10 first characters of description is the calendar date:
NSString * todayString = [[today description] substringToIndex:10];
NSString * yesterdayString = [[yesterday description] substringToIndex:10];
NSString * refDateString = [[refDate description] substringToIndex:10];

if ([refDateString isEqualToString:todayString]) 
{
    cell.title.text =  @"Today";
} else if ([refDateString isEqualToString:yesterdayString]) 
{
    cell.title.text =  @"Yesterday";
} else 
{
    cell.title.text =  refDateString;
}

If you want to change the format of the date string you could use descriptionWithLocale: instead.

Mo