Convert an array of Ints to a comma separated string

25,080

Well you can do it like :

let formattedArray = ([0,1,1,0].map{String($0)}).joined(separator: ",")
Share:
25,080
Lauren Eccles
Author by

Lauren Eccles

Updated on May 04, 2020

Comments

  • Lauren Eccles
    Lauren Eccles about 4 years

    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

  • Urvish Modi
    Urvish Modi over 6 years
    var stringIds = "" if let tmpList = self.userList { stringIds = (tmpList.map{$0.id}).map{String($0)}.joined(separator: ",") } user list is my custom objects array.
  • WestCoastProjects
    WestCoastProjects about 4 years
    You don't actually need the parens around the array/map [0,1,1,0].map{String($0)}.joined(separator: ",") is fine
  • asanli
    asanli about 3 years
    You are my Steve Jobs