我已经将文件夹复制到文档目录中,并且我能够枚举目录的内容,但是当我检查枚举的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)")
}
答案 0 :(得分:1)
absoluteString
是错误的API。您必须使用path
属性获取文件系统中的路径。
为了保持一致,请将网址命名为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)
...