Ios 如何将具体目录路径转换为NSURL?

Ios 如何将具体目录路径转换为NSURL?,ios,swift,swift4.2,Ios,Swift,Swift4.2,我想知道如何从路径字符串创建URL。 这是我的代码: let completePath = "/Volumes/MyNetworkFolder/" do { let items = try FileManager.default.contentsOfDirectory(atPath: completePath) for item in items { if item.hasDirectoryPath { //String

我想知道如何从路径字符串创建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而不是字符串

提前感谢您提供的任何帮助。

最好使用
FileManager
,它为您提供一个
URL
s数组,而不是 字符串:

不需要转换。告诉您这是否是一个目录。但正如MartinR所说,最好从一开始就使用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)
}