【发布时间】:2013-10-28 17:07:55
【问题描述】:
我正在从 json 对象创建图像文件,文件创建成功,图像确实创建并可以显示。我想要一个在下载图像之前检查文件是否仍在缓存或 tmp 目录中的进程。如果图像确实存在,那么我想检查文件的创建日期和修改日期属性,以查看是否需要下载图像的新版本以使其保持最新。这就是问题所在。
创建和修改日期属性为null。我什至尝试手动设置属性,但并没有改变任何东西。
这是我用来创建文件的代码:
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"tmp/%@",theFileName]];
BOOL filecreationSuccess =[fileManager createFileAtPath:fileName contents:myData attributes:nil];
if(filecreationSuccess == NO){
NSLog(@"Failed to create the file");
}
我什至尝试添加代码并且文件上的属性仍然是null:
NSDate *now = [NSDate date];
NSDictionary *createDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileCreationDate,nil];
NSDictionary *modDateAttr = [NSDictionary dictionaryWithObjectsAndKeys: now, NSFileModificationDate, nil];
[fileManager setAttributes:modDateAttr ofItemAtPath:fileName error:&err];
[fileManager setAttributes:createDateAttr ofItemAtPath:fileName error:&err];
我不确定下一步该做什么。任何帮助将不胜感激,谢谢。
【问题讨论】:
-
@trojanfoe 我将如何在那里设置创建日期属性?我找不到在
createFileAtPath函数中设置属性的实际示例。 -
我发现了我的问题。并不是没有设置修改和创建日期,而是我没有获得文件的正确属性集。结果是我使用的代码是获取文件所在路径的属性,而不是文件本身。 @trojanfoe 保留
attributes:nil仍默认为文件生成NSDate,并将修改日期的初始值设置为创建日期。
标签: ios6 nsfilemanager