为什么FileManager.default.attributesOfItem返回NSFileCreationDate中的当前日期?

问题描述 投票:1回答:1

尽管我已经检查了有关如何获得创建日期的其他答案,但似乎对我没有帮助。

这是我提取创建日期的方式。

class func extractFileCreatedDate(filepath: String) -> String{

    var dateString = ""
    do{

        let aFileAttributes = try FileManager.default.attributesOfItem(atPath: filepath) as [FileAttributeKey:Any]
        let theCreationDate = aFileAttributes[FileAttributeKey.creationDate] as? Date ?? Date()

        let formatter: DateFormatter = DateFormatter()
        formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale?
        formatter.timeZone = TimeZone.init(abbreviation: "GMT")
        formatter.dateFormat = "MM/dd/yyyy HH:mm:ss"

        //MARK:- Share App Submit Date
        if let readDate:String = formatter.string(from: theCreationDate){
            dateString = readDate
        }

    } catch let theError as Error{
        print("file not found \(theError)")
    }

    return dateString
}

所以我通过打印得到的答复是:

enter image description here

并且如果我检查此文件在本地存储中的创建日期是不同的:

enter image description here

也在调试中,我得到的是当前日期而不是创建日期:

enter image description hereenter image description here

如果我做错了,请建议我。我期望它应该返回相同的创建日期。或者也许我缺少一些东西,帮帮我。

谢谢!

ios swift nsfilemanager
1个回答
0
投票

creationDate将始终从我们的应用程序中返回文件的创建日期,而不是物理存储器(Mac)的创建日期。

用于测试:

1:将文件添加到包中(该文件是在过去的日期中创建的)并测试您的代码,它将返回您当前的日期(与现在一样)。

2:删除应用程序]手动将iPhone的日期更改为过去的任何日期。例如[<01-2019并运行该应用程序,您可以看到创建日期为01-01-2019

3:再次将日期更改为今天的日期并进行测试,您可以看到结果仍然是

01-01-2019

,因为创建日期是过去的日期希望它能帮助您!
© www.soinside.com 2019 - 2024. All rights reserved.