This question already has an answer here:
这个问题在这里已有答案:
- How do I convert a Swift Array to a String? 17 answers
- 如何将Swift数组转换为字符串? 17个答案
I know that if I want to convert an array of Int
s to a String
, I do this:
我知道如果我想将Ints数组转换为String,我会这样做:
[0,1,1,0].map{"\($0)"}.reduce(""){$0+$1}
but I cannot figure out how would I convert an array of Int
s to a comma separated String
但我无法弄清楚如何将Ints数组转换为逗号分隔的String
1 个解决方案
#1
25
Well you can do it like :
那么你可以这样做:
let formattedArray = ([0,1,1,0].map{String($0)}).joined(separator: ",")
#1
25
Well you can do it like :
那么你可以这样做:
let formattedArray = ([0,1,1,0].map{String($0)}).joined(separator: ",")