Skip to content

Instantly share code, notes, and snippets.

@quangDecember
Created May 15, 2019 19:28
Show Gist options
  • Save quangDecember/11e078c12d9b2540fe78c8703262e6f9 to your computer and use it in GitHub Desktop.
Save quangDecember/11e078c12d9b2540fe78c8703262e6f9 to your computer and use it in GitHub Desktop.
Construct URL using URLComponents, ensure safe path, percent encoding
extension URLComponents {
init(scheme: String = "https", host: String, path: [String], queries: [URLQueryItem]?) {
self = URLComponents.init()
self.scheme = scheme
self.host = host
var p = path
p.insert("/", at: 0)
self.path = NSString.path(withComponents: p)
self.queryItems = queries
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment