Skip to content

Instantly share code, notes, and snippets.

@myobie
Created July 17, 2019 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/f3393eaa7bf1498d8a624cde9fa4a5e9 to your computer and use it in GitHub Desktop.
Save myobie/f3393eaa7bf1498d8a624cde9fa4a5e9 to your computer and use it in GitHub Desktop.
Append query items just as easily as path components
extension URL {
func appendingQueryItems(_ items: [String: String]) -> URL {
guard var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else {
fatalError()
}
var query = components.queryItems ?? []
items.forEach { (name, value) in
query.append(URLQueryItem(name: name, value: value))
}
components.queryItems = query
guard let url = components.url else {
fatalError()
}
return url
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment