作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道如何从路径字符串创建 URL。这是我的代码:
let completePath = "/Volumes/MyNetworkFolder/"
do {
let items = try FileManager.default.contentsOfDirectory(atPath: completePath)
for item in items {
if item.hasDirectoryPath { //String has no member hasDirectoryPath
itemList.append(item)
}
}
} catch {
print("Failed to read dir")
let buttonPushed = dialogOKCancel(question: "Failed to read dir", text: "Map the network folder")
if(buttonPushed) {
exit(0)
}
}
我只想将文件夹添加到 itemList 数组。 hasDirectoryPath 是一种 URL 方法。我如何更改我的代码以获取 URL 而不是字符串。
提前感谢您提供的任何帮助。
最佳答案
最好使用 contentsOfDirectory(at url: URL, ...)
的方法FileManager
,它为您提供一个 URL
数组,而不是字符串:
let dirPath = "/Volumes/MyNetworkFolder/"
let dirURL = URL(fileURLWithPath: dirPath)
do {
let items = try FileManager.default.contentsOfDirectory(at: dirURL,
includingPropertiesForKeys: nil)
for item in items {
if item.hasDirectoryPath {
// item is a URL
// item.path is its file path as a String
// ...
}
}
} catch {
print("Failed to read dir:", error.localizedDescription)
}
关于ios - 如何将具体目录路径转换为 NSURL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442974/
我是一名优秀的程序员,十分优秀!