【发布时间】:2012-11-09 22:54:18
【问题描述】:
我正在使用这段代码来尝试检索文件的最后修改日期:
NSError *error = nil;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath: myFilePath error:&error];
if (attributes != nil) {
NSDate *date = (NSDate*)[attributes objectForKey: NSFileModificationDate];
NSLog(@"Date modiifed: %@", [date description]);
}
else {
NSLog(@"Not found");
}
这适用于主包中的文件,但如果文件位于应用程序文档文件夹的子目录中,myFilePath 如下所示:
/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/The App ID Number/Documents/mySubdirectory/My Saved File
它一直返回“未找到”。
我知道文件在那里,因为我可以使用 finder 来查看它。我也尝试删除文件名中的空格,但这没有效果。
错误日志显示没有这样的文件或目录,所以当我试图将文件复制到文档目录时,它看起来一定出了问题。
奇怪的是,使用contentsOfDirectoryAtPath 遍历文档子目录会显示文件存在。
我已尝试对路径进行硬编码并以编程方式检索它:
*myFolder = [documentsDirectory stringByAppendingPathComponent:@"myFolder"];
*myFilePath = [myFolder stringByAppendingPathComponent:theFileName];
谁能看出我哪里出错了?
【问题讨论】:
-
文件名中的那三个点是什么?
-
道歉;它们代表路径的其余部分,为简洁起见,我将其删除:
/6.0/Applications/The App ID Number/。我已经编辑了问题并重新插入了它们。 -
文件在你的xcode项目中吗?以编程方式创建?
-
@Tin Can:是的。然后我从文件夹中删除了它并手动添加了一个不同的文件,看看是否会有所作为。
-
@Robert Sherlock Holmes:“当你消除了不可能的事情时,剩下的无论多么不可能,都必须是事实。”您说您可以在 Finder 中看到该文件,因此请仔细检查您的文件路径。否则您的代码似乎没有问题。
标签: ios objective-c xcode last-modified nsdocumentdirectory