Last active
March 19, 2020 07:12
-
-
Save jackreichert/81a7ce9d0cefd5d1780f to your computer and use it in GitHub Desktop.
This is a swift extension for NSURL so you can parse the query string and get back a dictionary of the variables.
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
extension NSURL { | |
func getKeyVals() -> Dictionary<String, String>? { | |
var results = [String:String]() | |
var keyValues = self.query?.componentsSeparatedByString("&") | |
if keyValues?.count > 0 { | |
for pair in keyValues! { | |
let kv = pair.componentsSeparatedByString("=") | |
if kv.count > 1 { | |
results.updateValue(kv[1], forKey: kv[0]) | |
} | |
} | |
} | |
return results | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@i-schuetz your solution can lose information. a perfectly cromulent query might look like
?name=Alice&name=Bob&name=Ted&name=Carol
. If you ran your fragment on that queryItem listparams["name"]
would only returnCarol
Here's an extension to URL that works with Swift 4.2