Skip to content

Instantly share code, notes, and snippets.

@pedromancheno
Created January 16, 2019 13:06
Show Gist options
  • Save pedromancheno/766178efa916f5ff1d23da4a9c5da219 to your computer and use it in GitHub Desktop.
Save pedromancheno/766178efa916f5ff1d23da4a9c5da219 to your computer and use it in GitHub Desktop.
Using Swift's reduce method to construct a URL path with parameters
let params = [ "userID" : 1, "balance" : 1000 ]
let string: String = params.reduce("") {
if $0.isEmpty {
return $1.key + "=" + String($1.value)
} else {
return $0 + "&" + $1.key + "=" + String($1.value)
}
}
print("https://example.com?" + string)
// https://example.com?userID=1&balance=1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment