去掉 ?从查询字符串swift 4

时间:2019-09-27 17:50:34

标签: swift nsurlcomponents

我有一个URL,在其中添加URL组件以形成查询字符串。例如,URL为https://example.com/test,使用url组件,最终URL为https://example.com/test?urlcomponent1=1&urlcomponent2=1234

我需要使用urlcomponents保留最终的url,但是我需要删除?。我怎么做?因此,最终的网址为https://example.com/testurlcomponent1=1&urlcomponent2=1234

我已经研究过去除假象,但是还没有找到解决方法。

1 个答案:

答案 0 :(得分:1)

如果您知道只有一个?在您的网址中,您可以使用replaceOccurrencesOf

将其删除
let newURL = URL(string: url.absoluteString.replacingOccurrences(of: "?", with: ""))
相关问题