views:

123

answers:

1

Hi everyone,

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...

A: 

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.

greg
I don't do anything with lastModifDiff for the moment, it crashes at allocation, because lastModif seems to be nil according to the debugger.
Toto
I'm running your code now. I take out the unnecessary release and add an NSLog() after setting lastModifDiff. Both values are set for me. Are you sure your filename is correct?
greg
Yes, I can load it with NSKeyedUnarchiver, and [fm fileExistsAtPath:filename] is YES.
Toto
Run the code with the debugger and when it crashes type "where" into the console, maybe that will help us figure out what is going on. I copied and pasted your code into a new Xcode project, putting in a file path for something on disk, and I'm not crashing. Also try a clean build perhaps.
greg
Thank you very much, I don't really understand why, but it works now.
Toto