NSFileManager OS X上次修改日期

时间:2016-02-03 14:24:51

标签: swift macos nsfilemanager

我对以下代码有两个问题:

1)如何使用下面编码的NSFileManager获取目录的最后修改/更新日期?

2)阵列只打印出最后一条记录,如何在

中打印出结果
[
["ProjectName":"Project 1", "ProjectURL":"/Users/abc/Documents/MyProjectFolder/Project 1"],
["ProjectName":"Project 2", "ProjectURL":"/Users/abc/Documents/MyProjectFolder/Project 2"]
]

我的代码如下:

let documentsDirectoryURL =  try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)

var bool: ObjCBool = false

myPath = NSURL(string: String(documentsDirectoryURL) + "MyProjectFolder")
var filevar: [String:String] = [:]
    if NSFileManager().fileExistsAtPath(myPath.path!, isDirectory: &bool) {
        if bool.boolValue {
            let fileManager =  NSFileManager.defaultManager()
            let files = try! fileManager.contentsOfDirectoryAtURL(myPath, includingPropertiesForKeys: nil, options: [])
            for file in files {
                let filename = file.lastPathComponent
                let fileurl = file.path
                if filename != ".DS_Store"{
                    filevar = ["ProjectName":filename!, "ProjectURL": fileurl!]
                }
             }
             print("Result:\n \(filevar)")
         }
     }

2 个答案:

答案 0 :(得分:2)

1)从URL获取修改日期并比较重复循环中的日期。

2)创建一个数组而不是字典并附加字典。

let fileManager = FileManager.default
let documentsDirectoryURL =  try! fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

var isDir: ObjCBool = false

let myPath = documentsDirectoryURL.appendingPathComponent("MyProjectFolder")
var filevar = [[String:String]]()

var latestModificationDate = Date.distantPast
var latestFileURL = URL(fileURLWithPath: "/")
if fileManager.fileExists(atPath: myPath.path, isDirectory: &isDir) {
    if isDir.boolValue {
        do {
            let fileURLs = try fileManager.contentsOfDirectory(at: myPath, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
            for fileURL in fileURLs {
                let attributes = try! fileURL.resourceValues(forKeys:[.contentModificationDateKey, .nameKey])
                let filename = attributes.name!
                let modificationDate = attributes.contentModificationDate!
                if latestModificationDate.compare(modificationDate) == .orderedAscending {
                    latestModificationDate = modificationDate
                    latestFileURL = fileURL
                }
                filevar.append ( ["ProjectName":filename, "ProjectURL": fileURL.path])
            }
        } catch {
            print(error)
        }
        print("Result:\n \(filevar)")
        print(latestFileURL, latestModificationDate)
    }
}

选项.SkipsHiddenFiles可以避免检查.DS_Store

编辑:已更新至Swift 4

答案 1 :(得分:0)

快捷键4

以下是一些代码,可以从字面上获取特定URL的修改日期。注意:网址必须有方案(例如file://)。

func modifiedDate(atURL url: URL) -> Date? {
    if let attr = try? url.resourceValues(forKeys: [URLResourceKey.contentModificationDateKey]) {
        return attr.contentModificationDate
    }

    return nil
}