-timeIntervalSinceNow 的 EXC_BAD_ACCESS 错误
希望有人能帮忙解决这个问题。尝试使用 -timeIntervalSinceNow
方法时,我不断收到错误的访问错误。我在这个类中有一个名为 NSDate *startDate
的变量,并且我添加了 @property (nonatomic, keep) NSDate *startDate;
startDate
是在此处的代码中使用:
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];
当程序到达 [startDate timeIntervalSinceNow]
时,它会给出错误的访问错误。从我读过的有关该主题的其他帖子来看,答案通常似乎与保留日期有关。所以,我不确定我错过了什么。任何帮助将不胜感激!
Hoping someone can help with this. I keep getting a bad access error when trying to use the -timeIntervalSinceNow
method. I have a variable in this class called NSDate *startDate
and I've added @property (nonatomic, retain) NSDate *startDate;
startDate
is used in the code 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. From the other posts I've read on this topic, the answer usually seems to have something to do with retaining the date. So, I'm not sure what I'm missing. Any help would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
NSDate
在计时器触发之前自动释放。 更新:确保使用self.
声明的属性而不是实例变量。这将为您妥善处理保留问题。进而
Your
NSDate
was auto released before the timer fired. updated: Make sure you use the property that you declared instead of the instance variable by usingself.
. That will handle the retaining for you properly.and then
事实上,我最终找到了这个问题的答案。看起来使用
-timeIntervalSinceNow 比使用 -timeIntervalSinceNow 更好。它本质上做同样的事情,但由于某种原因 -timeIntervalSinceNow 每次都会给出一个错误的访问错误,但上面的方法工作得很好。
I actually figured out the answer to this, in the end. It seems like its better to use
rather than using -timeIntervalSinceNow. It essentially does the same thing, but for some reason -timeIntervalSinceNow gives a bad access error every time, but the method above works just fine.
[NSDate date]
返回一个自动发布的NSDate
。如果您想在返回值的方法之外使用该值,则必须保留
它(并在使用完后释放
它)。[NSDate date]
returns you an autoreleasedNSDate
. If you want to use that value outside of the method where it was returned to you, you have toretain
it (andrelease
it when you're done with it).