Skip to content

Instantly share code, notes, and snippets.

@hisoka0917
Created February 6, 2018 02:58
Show Gist options
  • Save hisoka0917/7474e1f76fb11f91199de36dfe4e25c4 to your computer and use it in GitHub Desktop.
Save hisoka0917/7474e1f76fb11f91199de36dfe4e25c4 to your computer and use it in GitHub Desktop.
Format Dictionary to url query
extension Dictionary {
func urlQueryEncode() -> String {
return self
.map({ "\($0)=\($1)" })
.joined(separator: "&")
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
}
func queryParameters() -> String {
let parameterArray = self.map { (key, value) -> String in
return String(format: "%@=%@",
String(describing: key).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!,
String(describing: value).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
}
return parameterArray.joined(separator: "&")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment