40

I know that if I want to convert an array of Ints to a String, I do this:

[0,1,1,0].map{"\($0)"}.reduce(""){$0+$1}

but I cannot figure out how would I convert an array of Ints to a comma separated String

3
  • @rmaddy In the question you linked as a duplicate, the accepted answer assumes an array of Strings and therefore would not have answered the OP's question; it was missing the map step. So it's not an exact duplicate IMHO. Commented Jun 21, 2019 at 20:12
  • @idz That's true only if you look at the currently accepted answer. There are many answers and several show how to convert an array of numbers. Commented Jun 21, 2019 at 20:15
  • @rmaddy Fair point! Commented Jun 21, 2019 at 20:16

1 Answer 1

120

Well you can do it like :

let formattedArray = [0,1,1,0].map{String($0)}.joined(separator: ",")
Sign up to request clarification or add additional context in comments.

4 Comments

var stringIds = "" if let tmpList = self.userList { stringIds = (tmpList.map{$0.id}).map{String($0)}.joined(separator: ",") } user list is my custom objects array.
You don't actually need the parens around the array/map [0,1,1,0].map{String($0)}.joined(separator: ",") is fine
You are my Steve Jobs
append missed ) at last sentence.. for fix it