Skip to content

Instantly share code, notes, and snippets.

@scotteg
Last active November 30, 2015 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scotteg/5893e6269ec7ceb21987 to your computer and use it in GitHub Desktop.
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.
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