Skip to content

Instantly share code, notes, and snippets.

@seanbehan
Created September 23, 2018 20:11
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 seanbehan/5acd8ed4b25b485885a03dc664757827 to your computer and use it in GitHub Desktop.
Save seanbehan/5acd8ed4b25b485885a03dc664757827 to your computer and use it in GitHub Desktop.
parse url params in swift with an extension
extension URL {
// Usage:
// URL(string:"http://.../?x=1&y=2").params()
func params() -> [String:Any] {
var dict = [String:Any]()
if let components = URLComponents(url: self, resolvingAgainstBaseURL: false) {
if let queryItems = components.queryItems {
for item in queryItems {
dict[item.name] = item.value!
}
}
return dict
} else {
return [:]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment