iOS:如何找到文件的创建日期?

Ben*_*ton 34 date file nsfilemanager ios

我正在尝试查找文件的创建日期(不是修改日期).

虽然修改日期是,但创建日期似乎不在文件的属性中.

我正在使用此代码..

NSFileManager* fm = [NSFileManager defaultManager];

NSString* path = [PathHelpers pathInDocumentsFolderWithFilename:FILE_NAME];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];

if (attrs != nil) {
    return (NSDate*)[attrs objectForKey: NSFileCreationDate];
} else {
    return nil;
}
Run Code Online (Sandbox Code Playgroud)

这总是返回零.在调试器中键入"po attrs"以获取NSDictionary中的键/值对列表将返回以下内容.

NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = 2010-01-21 11:47:55 +0000;
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = ben;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 338;
NSFileSystemFileNumber = 2234;
NSFileSystemNumber = 42553324;
NSFileType = NSFileTypeRegular;

没有创作日期..呸..

任何人都知道另一种获取创建日期的方式,还是只是在iOS中不存在?

Ori*_*eto 67

这段代码实际上给我留下了良好的创建日期:

NSFileManager* fm = [NSFileManager defaultManager];
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil];

if (attrs != nil) {
    NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate];
    NSLog(@"Date Created: %@", [date description]);
} 
else {
    NSLog(@"Not found");
}
Run Code Online (Sandbox Code Playgroud)

您是否在App中创建文件?也许这就是问题所在.


Rid*_*lly 17

有一个特殊的消息fileCreationDate对于在NSDictionary.以下适用于我:

Objective-C的:

NSDate *date = [attrs fileCreationDate];
Run Code Online (Sandbox Code Playgroud)

迅速:

let attrs = try NSFileManager.defaultManager().attributesOfItemAtPath(path) as NSDictionary
attrs.fileCreationDate()
Run Code Online (Sandbox Code Playgroud)

  • swift 版本工作正常,但在 swift 5 中(至少)没有名为 `attrs.fileCreationDate()` 的方法,而是我使用 `attrs[.creationDate] 作为?日期` ...如果你想知道 (3认同)

Cod*_*der 9

更新Swift 4 的答案以提取修改 ( .modifiedDate) 或创建 ( .creationDate) 日期:

let file: URL = ...
if let attributes = try? FileManager.default.attributesOfItem(atPath: file.path) as [FileAttributeKey: Any],
    let creationDate = attributes[FileAttributeKey.creationDate] as? Date {
    print(creationDate)
    }
Run Code Online (Sandbox Code Playgroud)
  1. 使用您通过 URL 预先提供的文件,它将请求其属性。如果成功,则返回 [FileAttributeKey: Any] 的字典
  2. 使用步骤 1 中的字典,然后提取创建日期(或根据您的喜好进行修改)并使用条件解包,如果成功则将其分配给日期
  3. 假设前两个步骤成功,您现在有一个可以使用的日期


Sam*_*Sam 8

Swift 2.0版本:

do {
    let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(YOURPATH)
    let creationDate = fileAttributes[NSFileCreationDate] as? NSDate
    let modificationDate = fileAttributes[NSFileModificationDate] as? NSDate
    print("creation date of file is", creationDate)
    print("modification date of file is", modificationDate)
}catch let error as NSError {
    print("file not found:", error)
}
Run Code Online (Sandbox Code Playgroud)


Nai*_*hta 6

在Swift 4中,如果您想知道DocumentsDirectory中特定文件的文件创建日期,则可以使用此方法

func getfileCreatedDate(theFile: String) -> Date {

        var theCreationDate = Date()
        do{
            let aFileAttributes = try FileManager.default.attributesOfItem(atPath: theFile) as [FileAttributeKey:Any]
            theCreationDate = aFileAttributes[FileAttributeKey.creationDate] as! Date

        } catch let theError as Error{
            print("file not found \(theError)")
        }
        return theCreationDate
    }
Run Code Online (Sandbox Code Playgroud)


mis*_*may 6

在 Swift 5 中:

let date = (try? FileManager.default.attributesOfItem(atPath: path))?[.creationDate] as? Date
Run Code Online (Sandbox Code Playgroud)