Skip to content

Instantly share code, notes, and snippets.

@dedeexe
Created November 27, 2015 17:26
Show Gist options
  • Save dedeexe/39a0f93774fb4ee401ef to your computer and use it in GitHub Desktop.
Save dedeexe/39a0f93774fb4ee401ef to your computer and use it in GitHub Desktop.
Convert a dictionary to string joining each key/value for a keyValueToken and mark each group by another token
extension Dictionary {
func joinKeyValuesFor(keyValueToken : String, groupedByToken:String ) -> String
{
var str = ""
for (key, value) in self
{
if str != ""
{
str += groupedByToken
}
str += "\(key)\(keyValueToken)\(value)"
}
return str
}
}
["user":"guest", "password":"12345", "grant":"password"].joinKeyValuesFor("=", groupedByToken: "&")
//Returns : "grant=password&user=dede&password=12345"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment