-timeIntervalSinceNow 的 EXC_BAD_ACCESS 错误

发布于 2024-11-29 05:24:42 字数 920 浏览 1 评论 0原文

希望有人能帮忙解决这个问题。尝试使用 -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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-12-06 05:24:42

您的 NSDate 在计时器触发之前自动释放。 更新:确保使用 self. 声明的属性而不是实例变量。这将为您妥善处理保留问题。

self.startDate = [NSDate date];

进而

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

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 using self.. That will handle the retaining for you properly.

self.startDate = [NSDate date];

and then

- (void)updatePlaybackPosition:(NSTimer *)timer {
   NSTimeInterval interval = [self.startDate timeIntervalSinceNow];
看海 2024-12-06 05:24:42

事实上,我最终找到了这个问题的答案。看起来使用

-timeIntervalSinceDate:[NSDate date]

-timeIntervalSinceNow 比使用 -timeIntervalSinceNow 更好。它本质上做同样的事情,但由于某种原因 -timeIntervalSinceNow 每次都会给出一个错误的访问错误,但上面的方法工作得很好。

I actually figured out the answer to this, in the end. It seems like its better to use

-timeIntervalSinceDate:[NSDate date]

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.

你げ笑在眉眼 2024-12-06 05:24:42

[NSDate date] 返回一个自动发布的 NSDate。如果您想在返回值的方法之外使用该值,则必须保留它(并在使用完后释放它)。

[NSDate date] returns you an autoreleased NSDate. If you want to use that value outside of the method where it was returned to you, you have to retain it (and release it when you're done with it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文