アプリの Documents フォルダー内の特定のファイルの作成日をテストすることに基づいたロジックを持つコードを書くことを計画していました。-[NSFileManager attributesOfItemAtPath:error:] を呼び出すと、NSFileCreationDate は提供された属性の 1 つではありません。
ファイルの作成日を知る方法はありませんか?
ありがとう。
アプリの Documents フォルダー内の特定のファイルの作成日をテストすることに基づいたロジックを持つコードを書くことを計画していました。-[NSFileManager attributesOfItemAtPath:error:] を呼び出すと、NSFileCreationDate は提供された属性の 1 つではありません。
ファイルの作成日を知る方法はありませんか?
ありがとう。
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;
}
Apple のリファレンスによると、NSFileCreationDate
は 2.0+ で利用可能です。
NSFileCreationDate 値がファイルの作成日を示すファイル属性ディクショナリのキー。
対応する値は NSDate オブジェクトです。
iPhone OS 2.0 以降で利用できます。
NSFileManager.h で宣言されています。