Difference between 2 dates in seconds ios

Solution 1:

since you are not using ARC, when you write

startTime = [NSDate date];

you do not retain startTime, so it is deallocated before -viewWillDisappear is called. Try

startTime = [[NSDate date] retain];

Also, I recommend to use ARC. There should be much less errors with memory management with it, than without it

Solution 2:

You should declare a property with retain for the start date. Your date is getting released before you can calculate the time difference.

So declare

@property (nonatomic, retain) NSDate *startDate

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setStartDate: [NSDate date]];
}

- (void)viewWillDisappear:(BOOL)animated
{
     [super viewWillDisappear:animated];
     NSLog(@"Seconds --------> %f",[[NSDate date] timeIntervalSinceDate: self.startDate]);
}

Don't forget to cleanup.

- (void)dealloc
{
    [self.startDate release];
    [super dealloc];
}