【发布时间】:2020-12-04 23:08:07
【问题描述】:
如何检查 Swift URL 是代表文件还是目录?
URL 对象上有一个hasDirectoryPath 属性,但它背后似乎没有任何“智能”。
它只是反映了传递给 URL 的 isDirectory 值。
代码:
let URL1 = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
print(URL1.hasDirectoryPath)
let URL2 = URL(fileURLWithPath: FileManager.default.currentDirectoryPath, isDirectory: true)
print(URL2.hasDirectoryPath)
输出:
false
true
【问题讨论】: