양홍범
양홍범

Reputation: 87

swift3 - replace url

I should replace download URL like "../~~~~.pdf.2.3"(.zip, .xls etc...) to "../~~~.pdf"

If I use url.lastPathComponent, returning nil.

So I did like this code.

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

It was worked but removingPercentEncoding not worked (return nil)

How can I bring encoded file name?

I cannot change this Server

Thank you

Upvotes: 0

Views: 94

Answers (1)

anatoliy_v
anatoliy_v

Reputation: 1800

Will this solution be fine for you?

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

Upvotes: 2

Related Questions