获取Swift 4中文档目录下所有“*.xxx”文件

问题描述 投票:0回答:1

我正在查看 Swift 4 中的

NSFileManager.h
dox,自从我上次使用它以来它似乎发生了变化(变得更好!)。

我想知道

contentsOfDirectoryAtURL:includingPropertiesForKeys
url
enumeratorAtURL:includingPropertiesForKeys
是否提供了一种枚举特定类型文件(在我的例子中为图像)的方法?

我查看了许多 Swift 2 答案,他们都建议循环并检查名称,我想确定他们没有“修复”这个问题,我只是对 API 感到困惑?

swift4 nsfilemanager
1个回答
13
投票

只是

filter
URLs
返回的
pathExtension

do {
    let docs = try FileManager.default.contentsOfDirectory(at: .documentsDirectory, includingPropertiesForKeys: nil, options:  .skipsHiddenFiles)
    let images = docs.filter{ $0.pathExtension == "xxx" }
    print(images)
} catch {
    print(error)
}
© www.soinside.com 2019 - 2024. All rights reserved.