Created
September 23, 2018 20:11
-
-
Save seanbehan/5acd8ed4b25b485885a03dc664757827 to your computer and use it in GitHub Desktop.
parse url params in swift with an extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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