代码之家  ›  专栏  ›  技术社区  ›  fs_tigre

如何在iOS中获取文档文件夹创建日期-Swift[副本]

  •  0
  • fs_tigre  · 技术社区  · 4 年前

    我不是一个专业的程序员,我做的应用程序是办公工具。我的最终目标是根据创建日期对NSURL对象数组进行简单排序。

    下面是我使用的代码,它的功能很好,但是如果我尝试使用注释行来计算特定的PDF文件,会出现以下错误:

    enter image description here

    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
        {
    
        }
    
    0 回复  |  直到 6 年前
        1
  •  11
  •   Leo Dabus    4 年前

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

    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)
    
        2
  •  2
  •   OOPer    8 年前

    根据单据头 URL URLResourceValues ,您可能需要这样写:

    (此代码假定 chosenURL 属于类型 URL? .)

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

    (如果您的 乔森乌尔 NSURL? ,请尝试此代码。)

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

    我建议你用 统一资源定位地址 NSURL ,尽你所能。

        3
  •  0
  •   mindeon    5 年前

    在swift 5中,我使用以下代码:

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

    使用以下代码对数组排序

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

    https://developer.apple.com/documentation/foundation/fileattributekey