获取Swift 4中文档目录中的所有"*.xxx"文件

Mau*_*itz 1 nsfilemanager swift4

我正在查看NSFileManager.hSwift 4中的dox,自从我上次使用它以来它似乎发生了变化(为了更好!).

在我的案例图片中,我想知道是否contentsOfDirectoryAtURL:includingPropertiesForKeys,urlenumeratorAtURL:includingPropertiesForKeys提供一种枚举特定类型文件的方法?

我看过很多Swift 2的答案,他们都建议循环和检查名称,我想确定他们没有"修复"这个,我只是对API感到困惑?

vad*_*ian 7

filter回来URLspathExtension

do {
    let documentsURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    let docs = try FileManager.default.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: [], options:  [.skipsHiddenFiles, .skipsSubdirectoryDescendants])
    let images = docs.filter{ $0.pathExtension == "xxx" }
    print(images)
} catch {
    print(error)
}
Run Code Online (Sandbox Code Playgroud)