2017-09-25 87 views
0

我應該將「../~~~~.pdf.2.3"(.zip,.xls etc ...」等下載網址替換爲「../~~~.pdf」swift3 - 替換url

如果我使用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])" 

它的工作,但removingPercentEncoding沒有奏效(返程無)

我如何幫助編碼後的文件名?

我不能改變這個服務器

謝謝

+0

什麼結果你在這裏'lastPathComponent' –

+0

lastPathComponent返回nil太 –

+0

你這裏得到的結果'remoteFileUrl.absoluteString' –

回答

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