Created
January 16, 2019 13:06
-
-
Save pedromancheno/766178efa916f5ff1d23da4a9c5da219 to your computer and use it in GitHub Desktop.
Using Swift's reduce method to construct a URL path with parameters
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
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