How can I get the file creation date using URL resourceValues method in Swift 3?

前端 未结 3 974
耶瑟儿~
耶瑟儿~ 2020-12-19 14:23

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 c

相关标签:
3条回答
  • 2020-12-19 14:27

    According to the header doc of URL and URLResourceValues, you may need to write something like this:

    (This code is assuming chosenURL is of type URL?.)

    do {
        if
            let resValues = try chosenURL?.resourceValues(forKeys: [.creationDateKey]),
            let createDate = resValues.creationDate
        {
            //Use createDate here...
        }
    } catch {
        //...
    }
    

    (If your chosenURL is of type NSURL?, try this code.)

    do {
        if
            let resValues = try (chosenURL as URL?)?.resourceValues(forKeys: [.creationDateKey]),
            let createDate = resValues.creationDate
        {
            //Use createDate here...
            print(createDate)
        }
    } catch {
        //...
    }
    

    I recommend you to use URL rather than NSURL, as far as you can.

    0 讨论(0)
  • 2020-12-19 14:34

    in swift 5 I use the following code:

    let attributes = try! FileManager.default.attributesOfItem(atPath: item.path)
    let creationDate = attributes[.creationDate] as! Date
    

    sort array with the following code

    fileArray = fileArray.sorted(by: {
            $0.creationDate.compare($1.creationDate) == .orderedDescending
        })
    

    more about FileAttributeKey here: https://developer.apple.com/documentation/foundation/fileattributekey

    0 讨论(0)
  • 2020-12-19 14:50

    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)
            }
        }
    }
    

    usage:

    print(yourURL.creationDate)
    
    0 讨论(0)
提交回复
热议问题