当我获取 NSFileModificationDate 时获取 EXEC_BAD_ACCESS

发布于 2024-08-26 08:22:10 字数 486 浏览 2 评论 0原文

我尝试获取文件的最后修改日期:

NSFileManager *fm = [[NSFileManager alloc] init];
NSError *err;
NSDate *lastModif = [[fm attributesOfItemAtPath:filename error:&err] objectForKey:NSFileModificationDate];//filename is ok ;-)
if(err == nil) {
    [lastModif retain];
    //I can put a NSLog of lastModif here, it works !!
    NSTimeInterval lastModifDiff = [lastModif timeIntervalSinceNow];//crash here
}

我不明白为什么 NSDate 似乎已发布,为什么保留不保留它。

谢谢你,如果你有什么想法...

I try to get the last modification date of a file:

NSFileManager *fm = [[NSFileManager alloc] init];
NSError *err;
NSDate *lastModif = [[fm attributesOfItemAtPath:filename error:&err] objectForKey:NSFileModificationDate];//filename is ok ;-)
if(err == nil) {
    [lastModif retain];
    //I can put a NSLog of lastModif here, it works !!
    NSTimeInterval lastModifDiff = [lastModif timeIntervalSinceNow];//crash here
}

I don't understand why the NSDate seems to be released, why the retain does not retain it.

Thank you if you have any idea...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

场罚期间 2024-09-02 08:22:10

您不需要保留lastModif。我认为当您使用它执行 NSLog 或之后对其执行任何操作时,您可能会尝试将 lastModifDiff 视为某种对象。 NSTimeInterval 是 double 的 typedef,因此您需要将其视为 double 或 [NSNumber numberWithDouble:lastModifDiff] 如果您想像对象一样使用它。

You don't need to retain lastModif. I think you might be trying to treat lastModifDiff as an object of some sort when you do an NSLog with it or whatever you do with it afterwards. NSTimeInterval is a typedef to a double so you need to treat it as a double or [NSNumber numberWithDouble:lastModifDiff] if you want to use it like an object.

思慕 2024-09-02 08:22:10

我遇到了同样的问题,但这篇文章似乎密切相关:

NSDate : timeIntervalSinceNow crash

我正在写一组简单的函数 - startClock/endClock - 使用 NSDate 确定游戏循环中的 FPS。除了 timeIntervalSinceNow 崩溃之外,声称我之前设置的 NSDate 对象不存在。

我知道,当我调用 startClock 时,NSDate 对象的保留计数为 1,但我的理论是,NSDate 实例在内部被操纵为当它们感到无聊并且感觉没有用时自动释放。

使用保留/释放来承担这些轻浮且短暂的 NSDate 对象的所有权对我来说很有效。

I'm having the same problem, but this post seemed germane:

NSDate : timeIntervalSinceNow crash

I'm writing a simple set of functions- startClock/endClock -using NSDate to determine FPS in my game loop. Except that timeIntervalSinceNow crashes, claiming that my earlier set NSDate object doesn't exist.

I know for a fact that the NSDate object has a retain count of 1 when I call startClock, but my theory is that NSDate instances are internally rigged to auto-release when they get bored and aren't feeling useful.

Using retain/release to assume ownership of these flighty and ephemeral NSDate objects worked for me.

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