Skip to content

Instantly share code, notes, and snippets.

@steve228uk
Created July 25, 2016 21:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steve228uk/c3cab4de42229c9a6eb3670b1898c00e to your computer and use it in GitHub Desktop.
Save steve228uk/c3cab4de42229c9a6eb3670b1898c00e to your computer and use it in GitHub Desktop.
Comma List Swift
extension SequenceType where Generator.Element == String {
func commaList() -> String {
var result = joinWithSeparator(", ")
let range = result.rangeOfString(", ", options: .BackwardsSearch)
result.replaceRange(range!, with: " & ")
return result
}
}
["1", "2", "3", "4"].commaList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment