I have a query item in a URL: q=Yellowstone+national+park. When using URLComponents to decode and extract the query value using the following, I get Yellowstone+national+park instead of Yellowstone national park.
let someURL = URL(string: "https://www.example.com/?q=Yellowstone+national+park")!
let components = URLComponents(url: someURL, resolvingAgainstBaseURL: true)
let keyword = components?.queryItems?.first(where: { (queryItem) -> Bool in
return queryItem.name == "q"
})?.value ?? "Keyword is empty"
I found URLComponents work fine with percent-encoded queries:
q=%E8%BF%87%E5%B1%B1%E8%BD%A6-->过山车
q=Yellowstone+national+park-->Yellowstone+national+park
Is there anyway URLComponents can automatically convert + to space ? Thanks!