【问题标题】:Detect file creation date on iPhone OS?检测 iPhone OS 上的文件创建日期?
【发布时间】:2011-01-28 10:33:15
【问题描述】:

我正计划编写一些代码,其逻辑基于测试我应用的 Documents 文件夹中特定文件的创建日期。事实证明,当我调用 -[NSFileManager attributesOfItemAtPath:error:] 时,NSFileCreationDate 不是提供的属性之一。

有没有办法发现文件的创建日期?

谢谢。

【问题讨论】:

  • 嘿,格雷格 - 已经 2 年了,接受怎么样? :D
  • 说实话,我不知道你的解决方案是否解决了我的问题! (我不记得了。)我遇到的问题不是访问创建日期的代码;而是访问创建日期的代码。只是创建日期没有作为文件属性之一返回。
  • 大声笑,伙计,你很坚强!

标签: iphone ios objective-c cocoa-touch nsfilemanager


【解决方案1】:

根据Apple's referenceNSFileCreationDate 在 2.0+ 中可用:

NSFileCreationDate 文件中的键 其值的属性字典 表示文件的创建日期。

对应的值是一个 NSDate 对象。

适用于 iPhone OS 2.0 及更高版本。

在 NSFileManager.h 中声明。

【讨论】:

  • 我同意它在文档中......但实际上,当您获取文件属性并查看所有这些属性时,它不是提供的属性之一。 (我不在电脑旁,否则我会把它们都列在这里。有 11 个;创建日期不是其中之一。)
  • 我应该补充一点,当我在模拟器上运行时就是这种情况。我还没有尝试在实际手机上运行它。今晚晚些时候我可以试试。
  • 奇怪。 NSFileManager 的 attributesOfItemAtPath:error: 应该返回一个包含所有这些的字典:developer.apple.com/library/ios/#documentation/cocoa/reference/… - 如果它不存在,那么你似乎不走运。应该是的。
【解决方案2】:

fileCreationDate 确实是字典的一部分。这是一个传递文件 URI 并从文件中获取一些属性的方法:

- (NSDictionary *) attributesForFile:(NSURL *)anURI {

// note: singleton is not thread-safe
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *aPath = [anURI path];

if (![fileManager fileExistsAtPath:aPath]) return nil;

NSError *attributesRetrievalError = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:aPath
                           error:&attributesRetrievalError];

if (!attributes) {
   NSLog(@"Error for file at %@: %@", aPath, attributesRetrievalError);
   return nil;
}

NSMutableDictionary *returnedDictionary = 
   [NSMutableDictionary dictionaryWithObjectsAndKeys:
        [attributes fileType], @"fileType",
        [attributes fileModificationDate], @"fileModificationDate",
        [attributes fileCreationDate], @"fileCreationDate",
        [NSNumber numberWithUnsignedLongLong:[attributes fileSize]], @"fileSize",
    nil];

return returnedDictionary;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多