如何在 Swift 3 中使用 URL resourceValues 方法获取文件创建日期? [英] How can I get the file creation date using URL resourceValues method in Swift 3?

查看:231
本文介绍了如何在 Swift 3 中使用 URL resourceValues 方法获取文件创建日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢本论坛中的各种有用帖子,我有一些代码可用于获取单个用户选择的 NSURL 的创建日期.但是,我无法使代码适用于硬编码的 NSURL,也无法通过 NSFileManager 枚举器在循环中工作.

Thanks to a variety of helpful posts in this forum, I have some code that works for obtaining the Creation Date of a single user-selected NSURL. However, I cannot get the code to work for either a hard-coded NSURL, nor within a loop through an NSFileManager enumerator.

我不是专业程序员;我制作的应用程序是办公工具.我的最终目标是简单地根据创建日期对 NSURL 对象数组进行排序.

I am not a professional programmer; I make apps that are tools for office. My ultimate goal is to simply sort an Array of NSURL objects based on Creation Date.

我正在使用的代码如下,其功能正常,但如果我尝试使用注释行来评估特定的 PDF 文件,我会收到以下错误:

The code I am using is below, which functions just fine as is, but if I try to use the commented line to evaluate a specific PDF file, I get the following error:

当我尝试将此代码添加到通过 NSFileManager 枚举器获取的 NSURL 对象循环时,我得到了完全相同的错误.

I get the exact same error when I try to add this code to a loop of NSURL objects procured via the NSFileManager Enumerator.

我不知道如何使用错误指令来解决问题.如果有人可以提供帮助,那将是巨大的.谢谢.

I cannot figure out how to use the error instruction to solve the problem. If anyone can assist, that would be tremendous. Thank you.

let chosenURL = NSOpenPanel().selectFile

    //let chosenURL = NSURL.fileURL(withPath: "/Users/craigsmith/Desktop/PDFRotator Introduction.pdf")

    do
    {
        var cr:AnyObject?
        try chosenURL?.getResourceValue(&cr, forKey: URLResourceKey.creationDateKey)

        if (cr != nil)
        {
            if let createDate = cr as? NSDate
            {
                print("Seems to be a date: \(createDate)")

                let theComparison = createDate.compare(NSDate() as Date)

                print("Result of Comparison: \(theComparison)")  // Useless

                let interval = createDate.timeIntervalSinceNow

                print("Interval: \(interval)")

                if interval < (60*60*24*7*(-1))
                {
                    print("More than a week ago")

                }
                else
                {
                    print("Less than a week ago")
                }
            }
            else
            {
                print("Not a Date")
            }
        }
    }
    catch
    {

    }

推荐答案

您可以按如下方式扩展 URL:

You can extend URL as follow:

extension URL {
    /// The time at which the resource was created.
    /// This key corresponds to an Date value, or nil if the volume doesn't support creation dates.
    /// A resource’s creationDateKey value should be less than or equal to the resource’s contentModificationDateKey and contentAccessDateKey values. Otherwise, the file system may change the creationDateKey to the lesser of those values.
    var creation: Date? {
        get {
            return (try? resourceValues(forKeys: [.creationDateKey]))?.creationDate
        }
        set {
            var resourceValues = URLResourceValues()
            resourceValues.creationDate = newValue
            try? setResourceValues(resourceValues)
        }
    }
    /// The time at which the resource was most recently modified.
    /// This key corresponds to an Date value, or nil if the volume doesn't support modification dates.
    var contentModification: Date? {
        get {
            return (try? resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
        }
        set {
            var resourceValues = URLResourceValues()
            resourceValues.contentModificationDate = newValue
            try? setResourceValues(resourceValues)
        }
    }
    /// The time at which the resource was most recently accessed.
    /// This key corresponds to an Date value, or nil if the volume doesn't support access dates.
    ///  When you set the contentAccessDateKey for a resource, also set contentModificationDateKey in the same call to the setResourceValues(_:) method. Otherwise, the file system may set the contentAccessDateKey value to the current contentModificationDateKey value.
    var contentAccess: Date? {
        get {
            return (try? resourceValues(forKeys: [.contentAccessDateKey]))?.contentAccessDate
        }
        // Beginning in macOS 10.13, iOS 11, watchOS 4, tvOS 11, and later, contentAccessDateKey is read-write. Attempts to set a value for this file resource property on earlier systems are ignored.
        set {
            var resourceValues = URLResourceValues()
            resourceValues.contentAccessDate = newValue
            try? setResourceValues(resourceValues)
        }
    }
}

用法:

print(yourURL.creationDate)

这篇关于如何在 Swift 3 中使用 URL resourceValues 方法获取文件创建日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆