swiftnsurlcomponents

Remove ? from query string swift 4


I have a url in which I add URL components to it to form a query string. For example the url is https://example.com/test, and with the url components the final url is https://example.com/test?urlcomponent1=1&urlcomponent2=1234.

I need to keep the final url with the urlcomponents, but I need to remove the ?. How do I do that? So the final url would be https://example.com/testurlcomponent1=1&urlcomponent2=1234.

I have looked into removing artifacts, but haven't found a solution.


Solution

  • If you know you only have one ? in your url, you can remove it using replacingOccurrencesOf

    let newURL = URL(string: url.absoluteString.replacingOccurrences(of: "?", with: ""))