Swift 3.1 macos:无法将字符串转换为 URL,Umlaute-Encoding

Swift 3.1 macos: Cant convert String to URL, Umlaute-Encoding

我现在阅读了很多关于转换和编码的内容,但我正在努力使它们正常工作: 我想将包含变音符号(例如ü,ä,ö)的字符串转换为URL,以执行removeItem(at: URL)。没有 Umlaute 也能完美工作。

pfadraus = "/Users/mdoe/Downloads/Test3 üKopie" 
let uhk = "file://\(pfadraus.replacingOccurrences(of: " ", with: "%20"))"
let uhk2 = URL(string: uhk)!

总是 returns 零,我怀疑应该进行一些编码,但我也很难设置它。如前所述,最后

let löschen = try FileManager.default.removeItem(at: uhk2)

如果您使用 dedicated method 将文件路径转换为 ​​URL:

,则无需转义任何特殊字符
let url = URL(fileURLWithPath: path)

但请注意,还有一个 removeItem(atPath:) 方法,因此您不需要转换为 URL 就是为了这个目的。