Created
July 25, 2016 21:02
-
-
Save steve228uk/c3cab4de42229c9a6eb3670b1898c00e to your computer and use it in GitHub Desktop.
Comma List Swift
This file contains 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
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