Last active
November 30, 2015 13:10
-
-
Save scotteg/5893e6269ec7ceb21987 to your computer and use it in GitHub Desktop.
Swift NSURL extension that returns a Dictionary of String keys and String values for each key/value pair in an NSURL query string.
This file contains 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
import Foundation | |
/** | |
Returns a `Dictionary` of `String` keys and `String` values for each key/value pair in an `NSURL` query string. | |
- author: Scott Gardner | |
- seealso: | |
* [Source on GitHub](http://bit.ly/SwiftQueryItemsDictionaryNSURLExtension) | |
*/ | |
extension NSURL { | |
var queryItemsDictionary: [String: String] { | |
var queryItemsDictionary = [String: String]() | |
guard let components = NSURLComponents(URL: self, resolvingAgainstBaseURL: false), queryItems = components.queryItems else { return queryItemsDictionary } | |
queryItems.forEach { queryItemsDictionary[$0.name] = $0.value } | |
return queryItemsDictionary | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment