Created
January 14, 2020 03:09
-
-
Save Yerazhas/dfd25455ed16f497897685a408db7daf to your computer and use it in GitHub Desktop.
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
var str = "Hello, playground" | |
let url = URL(string: "http://www.example.com?skey=a72bzy321bgf&id=123123")! | |
extension URL { | |
var queryItemsDict: [String: String] { | |
var dict = [String: String]() | |
guard let urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: false), let queryItems = urlComponents.queryItems else { return dict } | |
for queryItem in queryItems { | |
dict[queryItem.name] = queryItem.value ?? "" | |
} | |
return dict | |
} | |
} | |
print(url.queryItemsDict) | |
//prints ["id": "123123", "skey": "a72bzy321bgf"] | |
print(url.queryItemsDict["id"] ?? "notFound") | |
//prints 123123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicely done