0

如何访问文件修改日期

    NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:
                         @"TempiPad"];
    NSFileManager *localFileManager=[[NSFileManager alloc] init];
    NSDirectoryEnumerator *dirEnum =
    [localFileManager enumeratorAtPath:docsDir];
    NSString *file;

    // List all files in docsDir
    while (file = [dirEnum nextObject]) {
        NSLog(@"File = %@",file);
        NSError *attributesError = nil;
        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:file error:&attributesError];

        NSString *fileModificationDate = [fileAttributes objectForKey:NSFileModificationDate];
        NSLog(@"ModDate = %@",fileModificationDate);

        } 
    }

这只是输出(空)。最终我会想要比较两个文件的 fileModificationDate,那么我该怎么做呢?

4

2 回答 2

2

IIRC,您需要将文件名附加到目录路径;就像是:

while (file = [dirEnum nextObject]) {
    NSLog(@"File = %@",file);
    NSError *attributesError = nil;
    file = [docsDir stringByAppendingPathComponent:file];
    NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:file error:&attributesError];

    NSDate *fileModificationDate = [fileAttributes objectForKey:NSFileModificationDate];
    NSLog(@"ModDate = %@",fileModificationDate);

    } 
}

而且,正如更紧张的说法,NSFileModificationDate是一个NSDate,而不是一个NSString

于 2012-04-09T14:14:30.007 回答
0

NSFileModificationDate返回一个NSDate实例,而不是一个NSString. 看:

NSFileModificationDate

于 2012-04-09T14:10:09.303 回答