Home > Back-end >  How to remove the backlash from the end if the URL in Swift iOS
How to remove the backlash from the end if the URL in Swift iOS

Time:05-20

I am having a issue where I have to remove the backlash(/) from the end of the url. actually I had removed last path component by using deleting url.deletingLastPathComponent() and now generated url contains / at the end. So how can I remove it to make the url workable.

enter image description here

CodePudding user response:

This should work fine. Coming to required answer, we can't do string manipulation for URLs. Solution would be

  1. Remove last path component
  2. Remove /
  3. Create url again
    url.deleteLastPathComponent()
    var address = url.absoluteString

    address.removeLast()

    url = URL(string: address)

Spaces are being replaced by because of URL encoding. e.g.

var name = "Rajesh Budhiraja"
name.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

now name will be 'Rajesh Budhiraja'

you can undo this via

name.removingPercentEncoding
  • Related