0

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

4
  • what the result you get here lastPathComponent Commented Sep 25, 2017 at 9:27
  • lastPathComponent returned nil too Commented Sep 25, 2017 at 9:28
  • what the result you get here remoteFileUrl.absoluteString Commented Sep 25, 2017 at 9:29
  • classnet.test.co.kr/class/cnet-data/lecture_notice/2017-2/… Commented Sep 25, 2017 at 9:34

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.