Create a CSV from an array of strings in Swift in one line of code

I have an array of strings in swift, and I thought I could do:

array.join(",") to return a list of items separated by commas

The error I get is: Array<String> not convertible to 'String'

How can I do it correctly as little code as possible.

I can do this with a loop to build a string, but I thought there was an easier way to do this.

+5
source share
4 answers

In Swift 2 ,

 array.joinWithSeparator(",") 
+6
source

Given an array of strings:

 var x = ["one", "two", "three"] 

The correct syntax for concatenating strings is:

Swift 1.2

 ",".join(x) 

Swift 2.0

 x.joinWithSeparator(",") 
+10
source

In Swift 4

 let array:[String] = ["one", "two","three"] array.joined(separator: " ") 
+2
source

In Swift 3 ,

 values.joined(",") 
+1
source

Source: https://habr.com/ru/post/1202245/


All Articles