swift3 - 替换url

时间:2017-09-25 09:10:37

标签: ios url web swift3

我应该将" ../~~~~ .pdf.2.3"(。zip,.xls等...)等下载网址替换为" ../ ~~~ .PDF"

如果我使用url.lastPathComponent,则返回nil。

所以我确实喜欢这段代码。

let fileLastPathComponents = remoteFileUrl.absoluteString.components(separatedBy: "/")
    let lastPathComponent = fileLastPathComponents[fileLastPathComponents.count - 1]
    let fileName = lastPathComponent.components(separatedBy: ".")
    let fileNameStr = "\(fileName[0]).\(fileName[1])"

它已经工作但是removePercentEncoding不起作用(返回nil)

如何携带编码文件名?

我无法更改此服务器

谢谢

1 个答案:

答案 0 :(得分:2)

这个解决方案对你来说没问题吗?

func dropVersion(fromPath path: String) -> String {
    var path = path
    var lastComponent = (path as NSString).lastPathComponent
    path = (path as NSString).deletingLastPathComponent as String

    while lastComponent.characters.count > 0 && (lastComponent.characters.last == "." || Int(String(lastComponent.characters.last!)) != nil) {
        lastComponent = String(lastComponent.dropLast())
    }

    return path + "/" + lastComponent
}

let path = "/this_is/your/path.zip.2.3"
dropVersion(fromPath: path) // will return /this_is/your/path.zip