Error EXC_BAD_ACCESS for -timeIntervalSinceNow

Hope someone can help with this. When I try to use the -timeIntervalSinceNow method -timeIntervalSinceNow I get bad access. I have a variable in this class called NSDate *startDate , and I added @property (nonatomic, retain) NSDate *startDate;

startDate used here:

  startDate = [NSDate date]; updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updatePlaybackPosition:) userInfo:nil repeats:YES]; } 

}

 - (void)updatePlaybackPosition:(NSTimer *)timer { NSTimeInterval interval = [startDate timeIntervalSinceNow]; 

When the program reaches [startDate timeIntervalSinceNow] , it gives a bad access error. Of the other posts I read on this topic, usually the answer is usually related to saving the date. So I'm not sure what I am missing. Any help is appreciated!

+4
source share
3 answers

Your NSDate was automatically released before the timer started. updated: Make sure you use the property that you declared instead of the instance variable using self. . This will hold you right.

 self.startDate = [NSDate date]; 

and then

 - (void)updatePlaybackPosition:(NSTimer *)timer { NSTimeInterval interval = [self.startDate timeIntervalSinceNow]; 
+5
source

I actually understood the answer to this question in the end. It seems better to use

 -timeIntervalSinceDate:[NSDate date] 

instead of using -timeIntervalSinceNow. This essentially does the same thing, but for some reason, -timeIntervalSinceNow gives a bad access error every time, but the method above works just fine.

+1
source

[NSDate date] returns you autoreleased NSDate . If you want to use this value outside the method in which it was returned to you, you must retain it (and release it when you are done with it).

0
source

All Articles