Skip to content

Instantly share code, notes, and snippets.

@f-meloni
Created August 10, 2016 14:06
Show Gist options
  • Save f-meloni/906582ba8e0f5051e5fff9c7c8f4a907 to your computer and use it in GitHub Desktop.
Save f-meloni/906582ba8e0f5051e5fff9c7c8f4a907 to your computer and use it in GitHub Desktop.
NSURL swift extension that parses URL query string into [String: AnyObject] dictionary, using NSURLComponents and supporting array for values that uses ',' to separate array items
extension NSURL {
var queryDictionary: [String: AnyObject]? {
return NSURLComponents(URL: self, resolvingAgainstBaseURL: false)?
.queryItems?
.reduce([:], combine: { (var result: [String: AnyObject], queryItem) -> [String: AnyObject] in
if queryItem.value?.containsString(",") ?? false {
let array = queryItem.value?.componentsSeparatedByString(",")
result[queryItem.name] = array
}
else {
result[queryItem.name] = queryItem.value
}
return result
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment