Как я могу преобразовать конкретный путь к каталогу в NSURL? - PullRequest
0 голосов
/ 21 сентября 2018

Я хотел бы знать, как я могу создать 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-адреса не были строковыми.

Заранее благодарю за любую помощь, которую вы можете предоставить.

1 Ответ

0 голосов
/ 21 сентября 2018

Лучше использовать contentsOfDirectory(at url: URL, ...) метод FileManager, который дает вам массив URL s вместо строк:

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)
}
...