7

アプリの Documents フォルダー内の特定のファイルの作成日をテストすることに基づいたロジックを持つコードを書くことを計画していました。-[NSFileManager attributesOfItemAtPath:error:] を呼び出すと、NSFileCreationDate は提供された属性の 1 つではありません。

ファイルの作成日を知る方法はありませんか?

ありがとう。

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 に答える