【问题标题】:Get all the "*.xxx" files in the document directory in Swift 4在 Swift 4 中获取文档目录下的所有“*.xxx”文件
【发布时间】:2018-02-05 15:18:40
【问题描述】:

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

我想知道contentsOfDirectoryAtURL:includingPropertiesForKeysurlenumeratorAtURL:includingPropertiesForKeys 是否提供了一种方法来枚举特定类型的文件,在我的例子中是图像?

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

【问题讨论】:

  • contentsOfDirectoryAtURL 返回[URL]。您可以通过pathExtension z.B 来filter 网址。 .filter {$0.pathExtension == "xxx"}
  • 完美!如果你做出这个答案,我会投票赞成。

标签: swift4 nsfilemanager


【解决方案1】:

只是filterURLspathExtension返回的@

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

【讨论】:

    猜你喜欢
    • 2013-12-31
    • 2011-10-27
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多