let url = URL(string: "https://example.com")
let path = "/somePath?"
let urlWithPath = url?.appendingPathComponent(path)
After appending, the path /somePath?
becomes somePath%3F
.
The ?
becomes a %3F
. Question Mark is replaced with the percent-encoded escape characters.
The URL does output correctly if I use:
let urlFormString = URL(string:"https://example.com/somePath?")
Why does appendingPathComponent
convert ?
to %3F
?
How can I use appendingPathComponent
if the path component contains a question mark?
appendingPathComponent
, you shouldn’t include either the leading/
nor the trailing?
. If you want to add query to URL, you should consider usingURLComponents
. – Unconcerned