[英]Get EXEC_BAD_ACCESS when I get the NSFileModificationDate
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. 我不明白为什么NSDate似乎被发布,为什么保留不保留它。
Thank you if you have any idea... 谢谢,如果您有任何想法...
You don't need to retain lastModif
. 您无需保留
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. 我认为您在使用NSLog或之后对其进行任何处理时,可能会尝试将
lastModifDiff
视为某种对象。 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. NSTimeInterval
是一个double的typedef,因此,如果要将其用作对象,则需要将其视为double或[NSNumber numberWithDouble:lastModifDiff]
。
I'm having the same problem, but this post seemed germane: 我遇到了同样的问题,但是这篇文章似乎很贴切:
NSDate : timeIntervalSinceNow crash NSDate:timeIntervalSinceNow崩溃
I'm writing a simple set of functions- startClock/endClock -using NSDate to determine FPS in my game loop. 我正在编写一组简单的功能-startClock / endClock-使用NSDate确定游戏循环中的FPS。 Except that timeIntervalSinceNow crashes, claiming that my earlier set NSDate object doesn't exist.
除了timeIntervalSinceNow崩溃之外,声称我之前设置的NSDate对象不存在。
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. 我知道一个事实,当我调用startClock时,NSDate对象的保留计数为1,但是我的理论是,当NSDate实例感到无聊并且感觉不到用处时,会内部对其进行自动释放。
Using retain/release to assume ownership of these flighty and ephemeral NSDate objects worked for me. 使用保留/释放来假定这些飞行的和短暂的NSDate对象的所有权对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.