7

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

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

谢谢。

4

2 回答 2

8

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;
}
于 2011-05-20T22:04:43.797 回答
1

根据Apple 的参考NSFileCreationDate在 2.0+ 中可用:

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

对应的值是一个 NSDate 对象。

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

在 NSFileManager.h 中声明。

于 2010-03-17T06:02:12.873 回答