Created
August 10, 2016 14:06
-
-
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
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 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