使用时
let directoryEnumerator = FileManager().enumerator(at: ...
在Swift 3中,我从文件夹中获取所有文件,例如
"file:///Volumes/MacOS/fasttemp/Fotos/"
结果不包括前导路径(此处为“/ Volumes / MacOS”).所以我明白了
"file:///fasttemp/Fotos/2005/"
如何获取完整路径(直接来自枚举器)或转换它们.我想使用URL函数,而不是通过假设操作字符串函数.
如果“MacOS”是您当前启动盘的名称,则“/ Volumes / MacOS”是指向“/”的符号链接,因此“/ fasttemp / Fotos / 2005 /”和“/ Volumes / MacOS / fasttemp / Fotos /” “是同一文件的绝对路径.
为了获得唯一的文件名表示,您可以查询
其规范路径的URL.例:
let url = URL(fileURLWithPath: "/Volumes/MacOS/Applications/Utilities/") if let cp = (try? url.resourceValues(forKeys: [.canonicalPathKey]))?.canonicalPath { print(cp) } // Output: "/Applications/Utilities"
这需要macOS 10.12 / iOS 10或更高版本.在较旧的系统上,您可以
使用realpath()系统调用:
if let rp = url.withUnsafeFileSystemRepresentation ({ realpath($0,nil) }) { let fullUrl = URL(fileURLWithFileSystemRepresentation: rp,isDirectory: true,relativeTo: nil) free(rp) print(fullUrl.path) } // Output: "/Applications/Utilities"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。