2017-09-25 13 views
0

"../~~~.pdf.2.3"(.zip、.xlsなど)のようなダウンロードURLを "../~~.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

は ' –

+0

lastPathComponentがあまりにもnilを –

+0

を返さlastPathComponent'あなたがここに来る結果' 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 
関連する問題