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

前端 未结 3 985
耶瑟儿~
耶瑟儿~ 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.

提交回复
热议问题