【问题标题】:How do I read and open files in an iOS app's documents directory?如何读取和打开 iOS 应用程序文档目录中的文件?
【发布时间】:2018-04-13 04:18:27
【问题描述】:

我已将一个文件夹复制到文档目录中,并且我能够枚举目录的内容,但是当我检查枚举的 URL 是否存在并且是否可读时,我得到了错误。如何阅读和使用这些文件?

let imagesURL = copyPath.appendingPathComponent("images", isDirectory: true)
guard let fileEnumerator = FileManager.default.enumerator(at: imagesURL, includingPropertiesForKeys: nil, options: FileManager.DirectoryEnumerationOptions()) else { return }
while let file = fileEnumerator.nextObject() {
    guard let filepath = file as? NSURL else { continue }
    print(filepath.absoluteString!)
    let isReadable = FileManager.default.isReadableFile(atPath: filepath.absoluteString!)
    let exists = FileManager.default.fileExists(atPath: filepath.absoluteString!)
    print("isReadable: \(isReadable), exists: \(exists)")
}

【问题讨论】:

    标签: ios nsfilemanager


    【解决方案1】:

    absoluteString 是错误的 API。您必须使用 path 属性获取文件系统中的路径。

    为了保持一致,请将 URL 命名为 fileURL

    ...
    guard let fileURL = file as? URL else { continue }
    print(fileURL.path)
    let isReadable = FileManager.default.isReadableFile(atPath: fileURL.path)
    let exists = FileManager.default.fileExists(atPath: fileURL.path)
    ...
    

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多