How to construct URL with URLComponents and appendPathComponent in Swift

Issue #193

1
2
3
var components = URLComponents(string: "https://google.com/")
components?.path = "abc/"
components?.url

-> nil

1
2
3
4
var components = URLComponents(string: "https://google.com/")
components?.path = "/abc/"
components?.url
components?.queryItems = [URLQueryItem(name: "q", value: "pokemon")]

-> https://google.com/abc/?q=pokemon

1
2
3
var url = URL(string: "https://google.com/")
url?.appendPathComponent("/abc?q=pokemon")
url

-> https://google.com//abc%3Fq=pokemon

Comments